Jump to content
  • Advertisement
Sign in to follow this  
soconne

C++ class inheritance - iterating through chain of base clases

This topic is 3717 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

Is there a way to iterate through each class an object has inherited from? I know it requires some sort of custom scheme, but what is such a scheme called? I'm not looking for a full fledged RTTI implementation, all I would like is to see if a particular object has a certain class in its inheritance chain. For instance:
class Base {
}

class Next : public Base {
}

class Last : public Next {
}


Base* obj1 = new Last;
Base* obj2 = new Next;

if (obj1.Type.Contains(Last)) // do something
if (obj2.Type.Contains(Next)) // do something
ect...


Is there a standardized design pattern for doing this? [Edited by - soconne on July 19, 2008 1:13:39 AM]

Share this post


Link to post
Share on other sites
Advertisement
Just to clarify, are you looking for an implementation that avoids RAII constructs like dynamic_cast<> or what?

Its just I'm not sure how you got from this "I would like is to see if a particular object has a certain class in its inheritance chain" (simple dynamic_cast operation) to this "Is there a way to iterate through each class an object has inherited from" (complex in non-reflective languages like C++).

Share this post


Link to post
Share on other sites
No. Designs that place information into the class type itself are frowned upon; especially in a language like C++ with nil reflection capability.

Share this post


Link to post
Share on other sites
Quote:
Original post by Telastyn
No. Designs that place information into the class type itself are frowned upon; especially in a language like C++ with nil reflection capability.


Well that was essentially the approach I was thinking about. But I was thinking of having some base 'Object' class per say, that would store all inheritance chains for an object. Why is this frowned upon? Surely such a construct is needed in dozens of situations.

Oh, and could a moderator please move this thread to the General Programming forum? I mistakenly placed it in Game Programming.

Share this post


Link to post
Share on other sites
Quote:
Original post by soconne
Well that was essentially the approach I was thinking about. But I was thinking of having some base 'Object' class per say, that would store all inheritance chains for an object. Why is this frowned upon? Surely such a construct is needed in dozens of situations.


99% of practical cases can be solved elegantly using virtual member functions. The downside to explicit manipulation of types is that it violates the open/closed principle: you cannot extend the behavior of your program without altering its innards, which goes counter to the object-oriented philosophy.

Share this post


Link to post
Share on other sites
Because information should be data. Generally, you don't want a list of the inheritance tree you want a list of attributes or a dynamic list of functors that define some behavior.

Share this post


Link to post
Share on other sites
Quote:
Original post by Telastyn
Because information should be data. Generally, you don't want a list of the inheritance tree you want a list of attributes or a dynamic list of functors that define some behavior.


Well for my situation I'm not interested in the behavior. I'm only interested in what classes an object inherited from so I can perform a correct casting.

Share this post


Link to post
Share on other sites
Quote:
Original post by soconne
Well for my situation I'm not interested in the behavior. I'm only interested in what classes an object inherited from so I can perform a correct casting.


The standard solution to this specific problem, as already expressed by rip-off, is a language feature known as dynamic_cast. Anything else will just reinveint that particular wheel.

Of course, encountering this problem in the first place remains a sign of bad design.

Share this post


Link to post
Share on other sites
Quote:
Original post by soconne
Quote:
Original post by Telastyn
Because information should be data. Generally, you don't want a list of the inheritance tree you want a list of attributes or a dynamic list of functors that define some behavior.


Well for my situation I'm not interested in the behavior. I'm only interested in what classes an object inherited from so I can perform a correct casting.


To do what? Give us a concrete example.

Share this post


Link to post
Share on other sites
Quote:
Original post by Telastyn
To do what? Give us a concrete example.


See my original post. I gave a concrete code example.

Quote:
Original post by ToohrVyk
The standard solution to this specific problem, as already expressed by rip-off, is a language feature known as dynamic_cast.


Here's an example of why I can't use dynamic_cast.


class Base {
virtual void func() { }
};

class A : public Base {
};

class B : public A {
};

void main() {
Base* base = new A();

// This SHOULDN'T work, but it does
B* b = dynamic_cast<B*>(base);
}


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!