The following question might look like a duplicate but I couldn’t find the exact idea anywhere else in the site.
A brief introduction: This question raised as I was trying to understand a little more complexity inheritance combination in my class.
One note before I present the question – I don’t look for a sort of “Solution”, only wish to discuss with you about the topic and acquire a better thinking.
Suppose you have the following lines:
class C : public B
{
public :
C (const B& b) : B (b) {}
B& operator*() {return *this;}
};
int main() {
A* pA = new B();
C& c = pA -> doIt();
*c = *pA;
c = *pA;
return 1;
}
This code isn’t full and this is the whole purpose – assume it does complie and extract the constraints in this code.
Still I wouldn’t just write a code and ask you to explain any constraint of the top off your head but instead I will add my concreate question:
What constraints do we have for the following line :
C& c = pA -> doIt();
As far as I understand, the above code has one staring constraint which is the hierarchy order of these class : C is derived of B and B is dervied of A (correct me If I’m wrong..)
I was trying to actually define these classes while keeping the hierarchy I described above, and I couldn’t have doIt() to return a type of its derived class C. I can use incomplete type C& in the prototype of an A function but yet I cannot return an object of type C.
Thank you in advance,
SyndicatorBBB
Try this:
Note that the implicit assignment operator hides the base version, so we need explicit unhiding of the operator with the
usingdeclaration.