I am trying to create a system where one base class has a bunch of functions, then derived classes can inherit it and add more functions. However, all of these functions return the this
keyword and I need the return type in derived classes to be a pointer of their own class, not of the base class. What might be the best way of implementing this?
Preferably, the system would work such that functions can be called on a variable one after another in a single line. For example, testObject->SetPositionX(5)->SetPositionY(12)->SetRGB(24, 80, 50);
or even testObject->AddChild((new Foo)->SetBorderWidth(5)->SetBorderStyle(BorderStyle::dotted));
The issue, however, is that if a function is called that's defined in the base class instead of the derived class, it will return a pointer of the base class type and remove access to all of the derived class functions.
As a full demonstration,
#include <iostream>
class Base {
public:
Base* CommonFunc() {
std::cout << "Common function part of Base and all derived classes" << std::endl;
return this;
}
};
class Foo: public Base {
public:
Foo* UniqueFunc() {
std::cout << "Unique function of the Foo class" << std::endl;
return this;
}
};
int main() {
Foo* exampleFoo = new Foo();
exampleFoo->UniqueFunc()->UniqueFunc()->CommonFunc()/*->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] */
My solution so far has been to make the base functions virtual and rewrite their definition in every derived class. Not a large rewrite, they execute the same code by simply calling the inherited class in a single line like this Foo* Foo::CommonFunc() {Base::CommonFunc(); return this;}
. In the full demonstration, something like this:
#include <iostream>
class Base {
public:
virtual Base* CommonFunc() {
std::cout << "Common function part of Base and all derived classes" << std::endl;
return this;
}
};
class Foo: public Base {
public:
// Redefine inherited function with the proper return type
Foo* CommonFunc() {
std::cout << "Executed from Foo: ";
Base::CommonFunc();
return this;
}
Foo* UniqueFunc() {
std::cout << "Unique function of the Foo class" << std::endl;
return this;
}
};
int main() {
Foo* exampleFoo = new Foo();
exampleFoo->UniqueFunc()->UniqueFunc()->CommonFunc()->UniqueFunc(); // This full line now works because Foo::CommonFunc() is redefined to return a Foo* instead of a Base*
return 0;
}
/* Output:
Unique function of the Foo class
Unique function of the Foo class
Executed from Foo: Common function part of Base and all derived classes
Unique function of the Foo class */
This solution works and does fix the issue, but my project has grown such that the base class has 20 of these self returning functions, and there are 15 derived classes that each need to reroute those functions. If a new function is added to the base class, I need to add new code to every derived class which, while not too terrible, simply defeats the purpose of inheritance. If anyone can help think of a new solution, it would be greatly appreciated!
Base
a template, try CRTP.Base *p=…; Derived *d=p->CommonFunc();
, and that’s impossible.this
keyword" -- More common is to return*this
(by reference). It's one extra character in thereturn
statement, but saves one character with each chain (because you'd use.
instead of->
).