Let’s assume there is such class hierarchy:
class A //base class
class B //interface
class C : public A, public B
Then C object is created:
A *object = new C();
Is it possible to cast object to B ?
Important: I assume I don’t know that object is C. I just know that it implements interface B
No. This is not possible (direct casting from
A*toB*).Because the address of
AandBare at different locations inclass C. So the cast will be always unsafe and possibly you might land up in unexpected behavior. Demo.The casting should always go through
class C. e.g.Demo