Предпочтительно, чтобы система работала так, чтобы функции можно было вызывать для переменной одну за другой в одной строке. Например, testObject->SetPositionX(5)->SetPositionY(12)->SetRGB(24, 80, 50); или даже testObject->AddChild((new Foo)->SetBorderWidth(5)-> SetBorderStyle(BorderStyle::dotted)); Проблема, однако, в том, что если вызывается функция, определенная в базовом классе, а не в производном классе, она вернет указатель типа базового класса и лишит доступа ко всем функциям производного класса.
В качестве полной демонстрации
Код: Выделить всё
#include
class Base {
public:
Base* CommonFunc() {
std::cout UniqueFunc()*/; // This line doesn't compile with the last function uncommented because CommonFunc() returns a Base* instead of a Foo* and UniqueFunc() is not part of the Base class.
return 0;
}
/* Output:
Unique function of the Foo class
Unique function of the Foo class
Common function part of Base and all derived classes
[Can't call unique function again or else compile error] */
Код: Выделить всё
#include
class Base {
public:
virtual Base* CommonFunc() {
std::cout
Подробнее здесь: [url]https://stackoverflow.com/questions/78686617/inherited-functions-returning-self-reference-with-derived-type[/url]