473,395 Members | 1,977 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

About inheritance

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

Jul 23 '05 #1
2 1205
Tony Johansson wrote:
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.
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


Jul 23 '05 #2
Tony Johansson wrote:
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();

Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Stephan Diehl | last post by:
I have a question about metaclasses: How would be the best way to "merge" different metaclasses? Or, more precisely, what is the best way to merge metaclass functionality? The idea is basicly...
17
by: Andrew Koenig | last post by:
Suppose I want to define a class hierarchy that represents expressions, for use in a compiler or something similar. We might imagine various kinds of expressions, classified by their top-level...
3
by: arserlom | last post by:
Hello I have a question about inheritance in Python. I'd like to do something like this: class cl1: def __init__(self): self.a = 1 class cl2(cl1): def __init__(self): self.b = 2
2
by: Tony Johansson | last post by:
Hello Experts!! Here we use multiple inheritance from two classes.We have a class named Person at the very top and below this class we have a Student class and an Employee class at the same...
1
by: Tony Johansson | last post by:
Hello Experts! I have some questions about inheritance that I want to have an answer to. It says "Abstract superclasses define a behavioral pattern without specifying the implementation" I...
3
by: Quentin Huo | last post by:
Hi: C# doesn't support multiple inheritance but it supports interface.But I think these ways are different. For example, C++ supports multiple inheritance. I have two classess classA and...
18
by: Tom Cole | last post by:
I'm working on a small Ajax request library to simplify some tasks that I will be taking on shortly. For the most part everything works fine, however I seem to have some issues when running two...
14
by: JoeC | last post by:
I have been writing games and I also read about good programming techniques. I tend to create large objects that do lots of things. A good example I have is a unit object. The object controls...
3
by: Jess | last post by:
Hello, I've been reading Effective C++ about multiple inheritance, but I still have a few questions. Can someone give me some help please? First, it is said that if virtual inheritance is...
8
by: Tony Johansson | last post by:
Hello! I wonder can somebody explain when is it suitable to use these methods OnKeyUp, OnKeyDown and OnKeyPress because these raise an event. These are located in class UserControl. If these...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.