About inheritance

T

Tony Johansson

Hello Experts!!

Assume we have the following inheritance tree.
At the top we have the class Vehicle class and the subclass to vehicle is a
class called Engine_vehicle and
below this class we have three subclasses which are Car, Bus and Truck.

If I have a pointer variable to a base class Vehicle like this
Vehicle *vehicle_ptr;

If I now want to check if vehicle_ptr only point to an object of class
Engine_vehicle and only that class.
What happen with the value mp below if vehicle_ptr really points to a
subclass to Engine_vehicle for example Car?
I only want to use dynamic_cast and not typeid.

Engine_vehicle *mp;
if (mp = dynamic_cast<Engine_vehicle *>(vehicle_ptr)
{
//When I'm here I want to be sure that vehicle_ptr really points to an
object of class Engine_vehicle.
}

Many thanks

//Tony
 
R

Rolf Magnus

Tony said:
Hello Experts!!

Assume we have the following inheritance tree.
At the top we have the class Vehicle class and the subclass to vehicle is
a class called Engine_vehicle and
below this class we have three subclasses which are Car, Bus and Truck.

If I have a pointer variable to a base class Vehicle like this
Vehicle *vehicle_ptr;

If I now want to check if vehicle_ptr only point to an object of class
Engine_vehicle and only that class.
What happen with the value mp below if vehicle_ptr really points to a
subclass to Engine_vehicle for example Car?

It will point to that object. dynamic_cast succeeds if the object is of the
target type or any class derived from it.
I only want to use dynamic_cast and not typeid.

Why? typeid is exactly what you're searching for. It can test for exact
classes.
 
A

Andrew McDonagh

Tony said:
Hello Experts!!

Assume we have the following inheritance tree.
At the top we have the class Vehicle class and the subclass to vehicle is a
class called Engine_vehicle and
below this class we have three subclasses which are Car, Bus and Truck.

If I have a pointer variable to a base class Vehicle like this
Vehicle *vehicle_ptr;

If I now want to check if vehicle_ptr only point to an object of class
Engine_vehicle and only that class.

Why do you want to check its type?

What happen with the value mp below if vehicle_ptr really points to a
subclass to Engine_vehicle for example Car?
I only want to use dynamic_cast and not typeid.

Engine_vehicle *mp;
if (mp = dynamic_cast<Engine_vehicle *>(vehicle_ptr)
{
//When I'm here I want to be sure that vehicle_ptr really points to an
object of class Engine_vehicle.
}

Instead of having this code 'outside' of the Engine_Vehicle class, make
a Virtual function on the base class, then move the code into the
Engine_vehicle class and add any appropriate implementation to the
remaining Vehicle derived classes.

so the code becomes...

vehicle_ptr->doTheThing();
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,665
Latest member
salkete

Latest Threads

Top