B
Boris
I'm porting code from Windows to UNIX and ran into a problem with
dynamic_cast. Imagine a class hierarchy with three levels: class Level2
derives from Level1 which derives from Base. If you look now at this code:
Base *b = new Level2();
Level1 *l1 = dynamic_cast<Level1*>(b);
Should dynamic_cast return a valid pointer or 0? I wonder as Visual Studio
2005 returns a valid pointer while g++ 3.4.6 returns 0. Both compilers work
as expected when dynamic_cast<Level2*> is used but return different results
with the code above. Who is right?
The class hierarchy I'm talking about has some more classes per level.
That's why sometimes a dynamic_cast to a level 1 class is preferred as you
can then operate on various level 2 classes which are all derived from the
same level 1 class. If you have to downcast to the actual level 2 class you
end up writing the same code for several level 2 classes which I would like
to avoid.
Boris
dynamic_cast. Imagine a class hierarchy with three levels: class Level2
derives from Level1 which derives from Base. If you look now at this code:
Base *b = new Level2();
Level1 *l1 = dynamic_cast<Level1*>(b);
Should dynamic_cast return a valid pointer or 0? I wonder as Visual Studio
2005 returns a valid pointer while g++ 3.4.6 returns 0. Both compilers work
as expected when dynamic_cast<Level2*> is used but return different results
with the code above. Who is right?
The class hierarchy I'm talking about has some more classes per level.
That's why sometimes a dynamic_cast to a level 1 class is preferred as you
can then operate on various level 2 classes which are all derived from the
same level 1 class. If you have to downcast to the actual level 2 class you
end up writing the same code for several level 2 classes which I would like
to avoid.
Boris