C++ "missing feature"

Started by
9 comments, last by Helter Skelter 18 years, 8 months ago
Consider the following code:

class A {};
class B: public A {};

A * pPointer;
B * pPointer2;
B Data;

pPointer = &Data

pPointer2 = (Son *)pPointer; // pseudo code

I think it would be good if there was some sort of operator, applied on a pointer to a parent class, that will return a pointer to the son class. So instead of doing casting when assiging pPointer into pPointer2, there will be a type checking, so you would know that you assign a son class to a pointer to that son class of the same class and avoid the dangers of casting. Why there is no such feature in C++?
It's all about the wheel.Never blindly trust technoligy.I love my internal organs.Real men don't shower.Quote:Original post by Toolmaker Quote:Original post by The C modest godHow is my improoved signature?It sucks, just like you.
Advertisement
class A {};class B : public A {};A* p1 = new B;B* p2 = dynamic_cast<B*> (p1);


?
Quote:Original post by The C modest god
Why there is no such feature in C++?


What makes you assume there isn't such a feature?

struct cfoo {    virtual void make_me_polymorphic() {}    /* RTTI must be enabled and the class implement 1+ virtual functions for     * dynamic cast to work (e.g. the class must be polymorphic)     */};struct cbar : cfoo {};cfoo foo , *foo_ptr;cbar bar , *bar_ptr;//Example 1: Downcasting (allways safe)bar_ptr = &bar;foo_ptr = bar_ptr; //OK, bar_ptr should be a cbar which is-a cfoo//Example 2: Upcasting (unsafe, cfoo * MAY point at a cbar)foo_ptr = &bar;bar_ptr = static_cast< cbar * >( foo_ptr ); //OK, *foo_ptr is a cbarfoo_ptr = &foo;bar_ptr = static_cast< cbar * >( foo_ptr ); //ERROR, *foo_ptr isn't a cbar but bar_ptr points at it anyways (undefined behavior if used AFAIK)//Example 3: Upcasting (safe, uses RTTI, NULL pointer if not derived)foo_ptr = &bar;bar_ptr = dynamic_cast< cbar * >( foo_ptr ); //OK, bar_ptr == &barfoo_ptr = &foo;bar_ptr = dynamic_cast< cbar * >( foo_ptr ); //OK, bar_ptr == NULL//Example 4: Upcasting references (safe, uses RTTI, std::bad_cast thrown if not derived)cfoo & foo_reference_1 = bar;cbar & bar_reference_1 = dynamic_cast< cbar & >( foo_reference_1 ); //OK, foo_reference_1 is a barcfoo & foo_reference_2 = foo;cbar & bar_reference_2 = dynamic_cast< cbar & >( foo_reference_2 ); //"OK" (defined behavior), throws a std::bad_cast exception
Ok, thanks for the reply.
It's all about the wheel.Never blindly trust technoligy.I love my internal organs.Real men don't shower.Quote:Original post by Toolmaker Quote:Original post by The C modest godHow is my improoved signature?It sucks, just like you.
Note that, when creating class hierarchies like that, you should really make the destructor virtual (in fact, the compiler should warn you about that when appropriate). This makes the "make_me_polymorphic" unnecessary.

cu,
Prefect
Widelands - laid back, free software strategy
What is RTTI?
It's all about the wheel.Never blindly trust technoligy.I love my internal organs.Real men don't shower.Quote:Original post by Toolmaker Quote:Original post by The C modest godHow is my improoved signature?It sucks, just like you.
I believe it stands for Run Time Type Information. dynamic_cast compiles always, and does its type checking runtime, instead of static_cast which may not compile if the types are not in heirarchy.
Quote:Original post by Bonehed316
I believe it stands for Run Time Type Information.

Correct.
Quote:dynamic_cast compiles always, and does its type checking runtime, instead of static_cast which may not compile if the types are not in heirarchy.

Correct again, this allows apparently unrelated types a and b to be cast to each other on the premis that pointers to said types may point to a type ab which inherits from both a and b.
Then RTTI must be always on, at least if you wish to use inheritance.
Is there a setting in the visual compiler where you can switch on/off RTTI?
It's all about the wheel.Never blindly trust technoligy.I love my internal organs.Real men don't shower.Quote:Original post by Toolmaker Quote:Original post by The C modest godHow is my improoved signature?It sucks, just like you.
Quote:Original post by The C modest god
Then RTTI must be always on, at least if you wish to use inheritance.

If you want to use dynamic_cast. Most uses of inheritence use compile time type information (not run time type information). RTTI must allways be on to use dynamic_cast or typeid, as these require being able to deduce at runtime the concrete type of a variable from a simple pointer. Your compiler should generate errors if you try to use either of these constructs without RTTI enabled, although I've heard some horror stories with regards to VC++ compilers where it didn't (might have had to do with mixed RTTI/non-RTTI compiled code with regards to DLLs and EXEs IIRC).
Quote:Is there a setting in the visual compiler where you can switch on/off RTTI?

Yes. Don't know which, if any, versions of MSVC++ have it enabled by default, although I can say that GCC has RTTI enabled by default.

This topic is closed to new replies.

Advertisement