I have the following test program (demonstrates what I am really trying to accomplish).
Does anyone know if the following is possible without a reinterpret_cast<>?
struct B;
struct A {
A() { }
A( const B &b) { }
A( const B *b) { }
A( B *b) { }
A* operator=(const B *b) { }
A* operator=(B *b) { }
};
struct B {
B() { }
B( const A &a) { }
B( const B * b) { }
B( B *b) { }
B* operator=(const A *a) { }
B* operator=(A *a) { }
};
int main(int argc, char *argv[])
{
A *a = new A();
B *b = new B();
A *c = b;
return 0;
}
I tried doing every conversion operator I could, but I just can’t seem to get
A *c = b;
to not complain
error C2440: 'initializing' : cannot convert from 'B *' to 'A *'
No, it’s not possible, because a
Bis not anA. You either need to use inheritance to makeBa subclass ofA(so that aBis anA), or use composition and then refer to the appropriate subobject.