The question: is it possible convert a pointer(reference) to C into pointer(reference) to class A.
Not unless the classes are polymorphic i.e. have at least one virtual member function. In that case dynamic_cast can be used to side cast, as shown in StoryTeller's answer.
However, if that C
pointer(reference) points to a child that inherits A
, then you can first cast down to that child pointer, and then to A
.
c1 c;
C& cref = c;
A& aref = static_cast<c1&>(cref);
That's of course not necessarily ideal because you cannot convert just any arbitrary C
pointer whose concrete type is unknown.
I understand that best solution is to make just class C inherited from A
If you did this, then all C
pointers would be implicitly convertible to A
pointers.
but still it can cause some problems with class a1 (two base A).
To work around the problems, you need shared bases i.e virtual inheritance.
struct A {}; // abstract
struct B : virtual A {}; // abstract
struct C : virtual A {}; // abstract
struct a1 : B, C {}; // indirect inheritance of A
struct c1 : C {};
int main() {
c1 c;
C& cref = c;
A& aref = cref;
}