dynamic_cast – cpp

The casting of variables, is when you change one type of variable to another, for example to change a integer value to a double value. But when it comes to classes, casting is allot more fun. Because since a casting variable has to be compatible with the return casting type, the base class will need to be polymorphic for this to work (virtual functions within the base class).

The basis of a dynamic_cast syntax is as follows.

return variable = dynamic_cast<return type>(casting value)

To try and demonstrate how dynamic_cast(ing) works here is a example code broken down into chunks. Here is the base class, that has the virtual int returnX() method.

class baseClassDoesWork
{
  protected : 
     int x;
  public : 
     baseClassDoesWork()  { x = 55; };
     virtual int returnX() { cout << "Base class" << endl; return x;}
     void setX(int newXValue) { x = newXValue;}
};

and here is the sub class that will inherit the base class and re-do the virtual method

class subBaseClassDoesWork : public baseClassDoesWork
{
  public : 
      subBaseClassDoesWork() : baseClassDoesWork() {};
      int returnX() { cout << "Subclass" << endl; return x;}
};

here, there is no real difference between the two functions, because the demo is about how dynamic casting works, but I have added in some debugging code to say what class is being called.

Since a dynamic_cast will cast the virtual functions, then the polymorphism effects the casting and newer virtual function that is in the subclass still will call called. But the casting to the class other functions will not “follow” the cast. In the example below first we are creating a subclass and then creating a base class from a dynamic_cast of the subclass (as stated before the base class casting will still include the virtual function from the subclass), so that if you want to convert to another class that also inherited from the base class but not the subclass then you can now convert to the new class.

Here is the code.

#include <iostream>
 
using namespace std;
 
class baseClassDoesWork
{
  protected : 
     int x;
  public : 
     baseClassDoesWork()  { x = 55; };
     virtual int returnX() { cout << "Base class" << endl; return x;}
     void setX(int newXValue) { x = newXValue;}
};
 
class subBaseClassDoesWork : public baseClassDoesWork
{
  public : 
      subBaseClassDoesWork() : baseClassDoesWork() {};
      int returnX() { cout << "Subclass" << endl; return x;}
};
 
int main()
{
    // this one does work because of the virtual method in the base class.
    // but we can create a sub class of the base class first.
    subBaseClassDoesWork *subBase = new subBaseClassDoesWork();
    // convert it to the base class
    baseClassDoesWork *baseDoes = dynamic_cast<baseClassDoesWork*>(subBase);
 
    // check to see if the casting worked e.g. not equal to 0
    if (baseDoes == 0)
      cout << "Bad casting" << endl;
    else
      cout << baseDoes->returnX() << endl;
 
    // re-set the value of X
    baseDoes->setX(30);
 
    // convert it back again to the sub class and things are still great :).
    subBaseClassDoesWork *subBase2 = dynamic_cast<subBaseClassDoesWork*>(baseDoes);
    if (subBase2 == 0)
      cout << "Bad casting" << endl;
    else
      cout << subBase2->returnX() << endl;
 
    return 0;
}

The output would be

Subclass
55
Subclass
30

Where as this would not work because there is no virtual functions to really link the classes together (polymorphic), error included in the source where the error would be.

#include 

using namespace std;

class baseClassNotWork
{
protected :
int x;
public :
baseClassNotWork() { x = 0; };
int returnX() { return x;}
};

class subBaseClassNotWork : public baseClassNotWork
{
public :
subBaseClassNotWork() : baseClassNotWork() {};
int returnX() { return x;}
};

int main()
{
baseClassNotWork *baseNot = new baseClassNotWork();
//error: cannot dynamic_cast

Leave a Reply

Your email address will not be published. Required fields are marked *