Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to call the constructor of a super class, two classes away from the current class in C++

I have three classes that inherit as follows:

Class_A  
Class_B : public Class_A  
Class_C : public Class_B

Class_A contains a constructor:

public: Class_A(const char *name, int kind);

Class_B does not contain that constructor.

In Class_C I wish to invoke the constructor of Class_A. Something like:
Class_C(const char *name, int kind) : Class_A::Class_A(name,kind) {}

The problem is that I cannot add an intermediate constructor to Class_B, because Class_B is generated code that regenerates every time I make clean. So I cannot make any lasting changes to Class_B. Needless to say, the above line of the constructor in Class_C gives the error: "type 'Class_A' is not a direct base of 'Class_C'".

Is there a way that I may invoke the constructor of Class_A in the subclass Class_C, without requiring the same type of constructor in Class_B?

like image 850
John Gilmore Avatar asked May 11 '11 09:05

John Gilmore


1 Answers

If you can't change the code that generates B, then you are out of luck, AFAIK. But if the A class contains such a constructor, maybe you can get away with adding a simple member function that sets those two variables and call it from inside the C constructor? May not be as efficient as it gets, but atleast it works.

like image 185
Xeo Avatar answered Nov 05 '22 22:11

Xeo