Thread: Detecting Inherited Class Type

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Apr 2004
    Location
    Ohio
    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 02:12 PM.

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, 11: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