performance of dynamic_cast

Hello there,

consider this code:

class Base
{
virtual ~Base() {}
};

class Child: public Base
{
int val;
};

Now I have a Base pointer, which is created once and which is used to create Child objects. However, I need to downcast it to access Child::val. I could circumvent it by not creating one Base pointer but by creating several pointers of the child classes (I have more than one in the actual code).

// by downcasting
Base * pBase; // this pointer is only created once

if ( ... )
{
pBase = new Child;
int a = dynamic_cast<Child*>( pBase )->val;
}

// or maybe like this (no generic Base pointer)
if( ... )
{
Child * pChild = new Child;
int a = pChild->val;
}

In the first case, I only need to create ONE pointer, but I need to downcast it in every if block to access Child::val, while in the second case I need to create a new pointer in every if block, but I don't need to cast.

Downcasting here seems obvious, but what would you prefer?
[1121 byte] By [matthias_k] at [2007-11-18 22:16:45]
# 1 Re: performance of dynamic_cast
It all depends on the design. In this case, I do not see a need for dynamic_cast().

Kuphryn
kuphryn at 2007-11-9 0:34:51 >
# 2 Re: performance of dynamic_cast
int a = dynamic_cast<Child*>( pBase )->val;

Note that you can't directly access the casted pointer because when the dynamic cast fails,
The access is like NULL->val...

Should keep the result of the cast in a pointer, like:
Child* pChild = dynamic_cast<Child*>( pBase );
if(pChild )
a = pChild->val;

Regards,
Guy
Guysl at 2007-11-9 0:35:51 >
# 3 Re: performance of dynamic_cast
// by downcasting
Base * pBase; // this pointer is only created once

if ( ... )
{
pBase = new Child;
int a = dynamic_cast<Child*>( pBase )->val;
}

// or maybe like this (no generic Base pointer)
if( ... )
{
Child * pChild = new Child;
int a = pChild->val;
}

In the first case, I only need to create ONE pointer, but I need to downcast it in every if block to access Child::val, while in the second case I need to create a new pointer in every if block, but I don't need to cast.

Downcasting here seems obvious, but what would you prefer?
Well...given the above example...I would not see the need for assigning child classes to a base class pointer everytime. Thus, I would simply go with the second example...however, I get the kind of feeling that there is some code in-between missing...you create a child class and access its value member...thus, from the example it will contain the default value always... :confused:
Andreas Masur at 2007-11-9 0:36:53 >
# 4 Re: performance of dynamic_cast
All casting should preferably be avoided.

dynamic_cast is useful really as a run-time check when you are getting an unknown, possibly from a library, and wish to validate it first. We use it here together with dlsym() but in general I have found that on the many occasions you think you need dynamic_cast you do not.

In your case, can you not have

class Base
{
public:
virtual int getVal() const = 0;
};

class Child
{
public:
int getVal() const { return val; }
};
NMTop40 at 2007-11-9 0:37:50 >
# 5 Re: performance of dynamic_cast
No because the type of 'val' is still undefined at compile time because child is a template while base must not be a template. I forgot to mention that.

I have banned dynamic_cast now where it was possible. Thanks.
matthias_k at 2007-11-9 0:38:51 >
# 6 Re: performance of dynamic_cast
If it's a template then you should have said earlier, but it's difficult to tell you how to avoid the cast when we do not have enough information.
NMTop40 at 2007-11-9 0:39:50 >