Detecting Inherited Class Type

This is a discussion on Detecting Inherited Class Type within the C++ Programming forums, part of the General Programming Boards category; I've learned that I'm clueless when it comes to search engines because I can never find the information I want ...

  1. #1
    Professional Chef leeor_net's Avatar
    Join Date
    Apr 2004
    Location
    Charles Town, WV
    Posts
    147

    Detecting Inherited Class Type

    I've learned that I'm clueless when it comes to search engines because I can never find the information I want so here I am again asking for the help of fellow programmers. The title should say it all -- terrible, I know.

    Anyway, I'm new to using STL containers and, having written them off for far too long I've finally realized the error of my ways and have come to appreciate and rely upon the power, simplicity and error prevention that the STL creates for programmers.

    To that end, I've been pondering over this question for a bit.

    Suppose I have a class, Entity. I create a vector, std::vector<Entity> myVector.

    Now suppose that from Entity I've derived two additional Classes, Dog and Cat. I know that I can stuff those into my Entity Vector (I'm aware of slicing issues, suffice it to say that the derivities are purely logic differences in virtual functions).

    Assuming that I've correctly initialized everything and populated the list randomly with Cat's and Dog's how, then, would I go about determining which type of derived Entity I'm using (Dog/Cat)? Is there a simple way of saying something like (warning, terrible psuedo code):

    Code:
    if(myVector[0] == Dog)
        do this
    else if(myVector[0] == Cat)
        do that
    else
       barf with some uselessly vague message
    Or is this something I have to manage in a different way?

    Thanks for taking the time to read this! Looking forward to some guidance.
    Last edited by leeor_net; 03-01-2009 at 01:12 PM.
    - Leeor

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,744
    Quote Originally Posted by leeor_net
    Suppose I have a class, Entity. I create a vector, std::vector<Entity> myVector.

    Now suppose that from Entity I've derived two additional Classes, Dog and Cat. I know that I can stuff those into my Entity Vector (I'm aware of slicing issues, suffice it to say that the derivities are purely logic differences in virtual functions).
    Since you want to use polymorphism, and polymorphism in C++ works with pointers and references, you should use a std::vector<Entity*>, std::vector<std::tr1::shared_ptr<Entity> >, or boost::ptr_vector<Entity> instead.

    Quote Originally Posted by leeor_net
    Assuming that I've correctly initialized everything and populated the list randomly with Cat's and Dog's how, then, would I go about determining which type of derived Entity I'm using (Dog/Cat)?
    You would not go about determining which derived type each object is. Rather, you would just call the virtual functions and let the virtual call mechanism perform the magic of calling the correct function for you, i.e., you would take advantage of polymorphism.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    However if you place a base class pointer in the container you will have to do a dynamic cast to specifically call derived functions that are not in the base. This will not be a problem if your derived classes do not expand on the interface of the base.

    Anytime you are writing code that tries to make 1 object act as if it is more than 1 or becomes all objects to all parts of the code you are not writing object oriented code. Your pseudo-code is a perfect example of a non-object oriented approach.

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by Bubba View Post
    Anytime you are writing code that tries to make 1 object act as if it is more than 1 or becomes all objects to all parts of the code you are not writing object oriented code. Your pseudo-code is a perfect example of a non-object oriented approach.
    To expand on Bubba's comment, designing code this way isn't wrong, it's just not how people typically do it (or recommend) in C++.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Agreed. I'm certainly not saying which is 'right' or 'wrong' just that one approach is object-oriented and one is not.

  6. #6
    Professional Chef leeor_net's Avatar
    Join Date
    Apr 2004
    Location
    Charles Town, WV
    Posts
    147
    Thank you for your replies -- always insightful.

    For the most part I don't care what kind of object the entity is -- the way I put them together they don't have additional functions, they just overload virtual functions in the base class that determine their behavior, logic and animation.

    In this case I was thinking about a special case of an entity in which its behavior is completely different -- e.g., a static object versus a dynamic object (not in the sense of C++ keywords but in the way the environment is set up, static objects never move, they may only animate themselves, dynamic objects move around).

    I figured I'd be sticking those special case objects into their own list as they are handled differently by the code that makes use of them (there's never a need to check the movement delta becuase they don't move, instead just call the update() function which does the rest).

    Like I stated in my previous post, the psuedo code was terrible. I don't know think it's even possible to write code like that in any language let alone C++. Thanks for the help, I'm pointed in the right direction now!
    - Leeor

  7. #7
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Actually you can write code like that in C++ using typeid() bit I don't recommend it.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    You can also do it using dynamic_cast, which is the better choice usually.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Not shoving things that cannot be used polymorphically into the same container (or hierarchy) sounds quite reasonable.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  10. #10
    Professional Chef leeor_net's Avatar
    Join Date
    Apr 2004
    Location
    Charles Town, WV
    Posts
    147
    I'd rather not use typeid() -- I was mostly wondering about handling special cases of an Entity but I think it's easiest to just use a list of a specific type of Entity (in this case it's called a Doodad -- it just sits in one spot looking pretty).
    - Leeor

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Though implementation problem
    By Elysia in forum C++ Programming
    Replies: 296
    Last Post: 05-31-2008, 01:02 PM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21