Possible Duplicate:
Finding the type of an object in C++
Hello,
I am sorry if it’s a duplicate but I was not able to find answer to my question here.
Let’s assume we have following class structure in c++:
class CPolygon {
protected:
int width, height;
public:
void set_values (int a, int b)
{ width=a; height=b; }
};
class CRectangle: public CPolygon {
public:
int area ()
{ return (width * height); }
};
Now I have got a pointer to CPolygon object. How do I check if it’s actually a pointer to the object of class CRectangle?
You can do this by checking if
dynamic_cast<CRectangle*>(ptr)return non-null, whereptris a pointer toCPolygon. However this requires the base class (CPolygon) to have at least one virtual member function which you probably need anyway (at least a virtual destructor).