Hi All,

I am working on adding additional functionality to an existing legacy code. In that code, I have a function which returns a pointer to an object of a parent class. I know that this object is actually a subclass of the parent class, and would like to access some of the methods from that subclass. Will using reinterpret_cast on that pointer to the object allow me to safely access the methods from this subclass? Just in case, is there something that I can use in the code to be 100% sure that this object is in fact of the subclass I believe it to be?

Thanks!

Elana

Why don't you use dynamic_cast. E.g.

class Base {};
class A : public Base {}; 
class B : public Base {};

Base* Aptr=new A();
Base* Bptr=new B();

A* Ax=dynamic_cast<A*>(Aptr);   // sets Ax to a Ax pointer. 
A* Ay=dynamic_cast<A*>(Bptr);   // sets Ay to ZERO
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.