Jump to content
  • Advertisement
Sign in to follow this  
The C modest god

C++ "missing feature"

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 cbar
foo_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 == &bar
foo_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 bar
cfoo & foo_reference_2 = foo;
cbar & bar_reference_2 = dynamic_cast< cbar & >( foo_reference_2 ); //"OK" (defined behavior), throws a std::bad_cast exception


Share this post


Link to post
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

Share this post


Link to post
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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!