informa
/
Programming
Blogs

Fun with dynamic cast.

Having fun with dynamic cast alternatives.

[ From the blog

Polymorphism allows for different function implementations to be called, depending on values only known at run time. For instance:

class BaseA {
 public:
 virtual void foo(){cout<<"Base";};
};
class A: public BaseA {
 public:
 void foo() {cout<<"Child";};
};
A a;
BaseA & base = a;
base.foo(); 

Will print "Child".

"Pair polymorphism" is when you want a specific function implementation to be called based on the run time value of two different instances of a class. For instance in the case of:

class BaseA {};
class BaseB {
 public:
 virtual void foo (BaseA & a) = 0;
};
class A: public BaseA {};
class B: public BaseB {
 public: void foo (A & a);
}; 

You would want that somehow "void B::foo (A & a)" will be called. The code I posted does not do this.
There is a way to do this, but it requires for a class to know about the implementation classes. It is not a problem in some cases. However, sometimes you want to have an abstraction layer.
An abstraction layer is a set of classes or interfaces, that have no methods or "don't know" anything about the classes that implement them. For instance you can have a graphics abstraction layer.
You could implement this layer with DirectX. However, this layer will make it easier to afterwards add an implementation with OpenGL without having to make a lot of changes, even if the game is already completed.

How are we going to have a "pair polymorphism" in this case? One solution is using dynamic casting. You can cast a pointer of a parent class to it's child class(in case the run time value really points to an instance of that class).
Some code:

class BaseA {};
class BaseB {
 public:
 virtual void foo (BaseA & a) = 0;
};
class A: public BaseA {};
class B: public BaseB {
 public:
 void foo (BaseA & a) {
 foo2 (dynamic_cast(a));
 }
 void foo2 (A & a);
}; 

I was asked by someone how this can be done without dynamic casting, because he doesn't like dynamic casting. I found a way, but it ain't pretty, and I honestly can't find any advantage of using this code instead of just using dynamic casting. But here it is:

class BaseA {
 public:
 virtual void DynamicCast() = 0;
};
class BaseB {
 public:
 virtual void foo (BaseA & base) = 0;
};
class A;
class Caster {
 public:
 A * a;
};
class A: public BaseA {
 public:
 Caster * c;
 void DynamicCast() { c->a = this; }
};
class B: public BaseB {
 public:
 Caster * c;
 void foo (BaseA & base) {
 base.DynamicCast();
 foo2 (c->a);
 }
 void foo2 (A & a);
}; 

Latest Jobs

Sucker Punch Productions

Bellevue, Washington
08.27.21
Combat Designer

Xbox Graphics

Redmond, Washington
08.27.21
Senior Software Engineer: GPU Compilers

Insomniac Games

Burbank, California
08.27.21
Systems Designer

Deep Silver Volition

Champaign, Illinois
08.27.21
Senior Environment Artist
More Jobs   

CONNECT WITH US

Register for a
Subscribe to
Follow us

Game Developer Account

Game Developer Newsletter

@gamedevdotcom

Register for a

Game Developer Account

Gain full access to resources (events, white paper, webinars, reports, etc)
Single sign-on to all Informa products

Register
Subscribe to

Game Developer Newsletter

Get daily Game Developer top stories every morning straight into your inbox

Subscribe
Follow us

@gamedevdotcom

Follow us @gamedevdotcom to stay up-to-date with the latest news & insider information about events & more