Inheritance in C++

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

Recommended Posts

Let's say I have a class, Organism. I have two classes that inherit from this class, Plant and Animal. I have a class Frog which inherits from Animal.
class Organism;

class Plant : public Organism;
class Animal : public Organism;

class Frog : public Animal;

In native C++, how do I determine whether a Frog is a Plant or an Animal? I've just found the typeid() function which can give me the class name, but apparently not its parent type?

Share on other sites
A Frog is never a Plant in this example. Your hierarchy of classes looks like:
  Organism  ^      ^  |      |Plant  Animal         ^         |       Frog

The lines represent inheritance ('is-a') relationships when read from bottom to top, so Frog is-a Animal, Animal is-a Organism, but there is no such relationship between Plant and Frog whatsoever.

(Now, perhaps you meant the opposite -- given an Animal, or perhaps an Organism, how do you determine what type it really is? In that case you can use dynamic_cast. However, in general the need to do this suggests a flaw in your design. Perhaps you should provide more context for why you need this question answered, and we can suggest alternatives that are superior to this attempt.)

Share on other sites
what I think you're looking for is dynamic_cast. Dynamic_cast is standard C++, and will return NULL if the pointer cannot be cast as the type requested.try this code:

Animal* a=dynamic_cast<Animal*>(aFrogPointer);
if(a!=NULL)
{
//it's an animal!
}
else
{
//it's not an animal (infer it's a plant)
}

This does require RTTI to be enabled in your compiler.

cheers,

Bob

Share on other sites
Quote:
 Original post by jpetrieA Frog is never a Plant in this example.
I just wanted to make this explicitly clear - you don't need to determine whether a Frog is an Animal or a Plant because you know a Frog is an Animal. [smile]

Share on other sites
To take the Source engine as an example, they put a virtual function in the base class that lets you determine what it is in the appropriate child.

IE:

"virtual bool IsPlayer() { return false; }" is defined in the base entity class. In the player class, "virtual bool IsPlayer() { return true; }" is defined.

Share on other sites
I think C++ assumes you already know the answer, so it doesn't provide that kind of information directly (doing so would unnecessarily clutter the language). You can use RTTI to get it indirectly or you can explicitly give your class an "int m_type" and do poor man's RTTI yourself.

I remember the old borland pascal had a parent reference, so you could call an object's parent functions without explicitly naming the parent class. It made it easier to insert classes in the middle of a hierarchy.

Share on other sites
Looks like dymanic_cast is what I'm looking for. I don't actually have an instance of where I'd need to use it, but if I recall correctly the functionality is available in C#, where I'm used to doing OO programming :) thanks for your replies

Share on other sites
This is fairly easy.

if is_base_of<Animal, Frog>::value is true, Animal is a base of Frog, otherwise it's not.

It's called meta-programming.

http://www.boost.org/doc/libs/1_37_0/libs/type_traits/doc/html/boost_typetraits/reference/is_base_of.html

1. 1
Rutin
26
2. 2
3. 3
4. 4
JoeJ
18
5. 5

• 14
• 14
• 11
• 11
• 9
• Forum Statistics

• Total Topics
631757
• Total Posts
3002127
×