I have a problem. There are two classes:
struct Base {
Base* retain() {
//retain here
return this;
}
};
struct Derived : Base {
};
Derived *d1 = new Derived();
Derived *d2 = d1->retain(); //error here: need to typecast to Derived*
Derived *d3 = (Derived*)d1->retain(); //OK
Is there any way to rewrite retain() function in a way that I don’t need to manually typecast result? In other words: retain() should return an object of the derived type.
Alternatively: