# C++ "missing feature"

This topic is 4829 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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++?

##### Share on other sites
class A {};class B : public A {};A* p1 = new B;B* p2 = dynamic_cast<B*> (p1);

?

##### Share on other sites
Quote:
 Original post by The C modest godWhy 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

##### Share on other sites
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

What is RTTI?

##### Share on other sites
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.

##### Share on other sites
Quote:
 Original post by Bonehed316I 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.

##### Share on other sites
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?

##### Share on other sites
Quote:
 Original post by The C modest godThen 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.

1. 1
2. 2
3. 3
4. 4
Rutin
21
5. 5

• 9
• 13
• 19
• 14
• 9
• ### Forum Statistics

• Total Topics
632936
• Total Posts
3009314
• ### Who's Online (See full list)

There are no registered users currently online

×