Is there a way to tell what class an object is?

This is a discussion on Is there a way to tell what class an object is? within the C++ Programming forums, part of the General Programming Boards category; Is there a way to tell what type of class an object is? I want to tag objects of a ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    40

    Is there a way to tell what class an object is?

    Is there a way to tell what type of class an object is?

    I want to tag objects of a few types with an id integer, and I want the classes to have seperate id index counters. The problem I'm running into is with the base class, as it will increment the id index even though the object is not of the appropriate class.
    Code:
    #define OBJECT1 0
    #define OBJECT2 1
    #define OBJECT3 2
    
    unsigned short int object1_count = 0, object2_count = 0, object3_count = 0;
    
    class object1
    {
      public:
      unsigned short int type, id;
      object1(void)
      {
        type = OBJECT1; id = object1_count++;
      }
    };
    class object2: public object1
    {
      public:
      object2(void)
      {
        type = OBJECT2; id = object2_count++;
      }
    };
    class object3: public object1
    {
      public:
      object3(void)
      {
        type = OBJECT3; id = object3_count++;
      }
    };
    As you can see, object1_count will be incremented even when it's not used. I want object1_count to be incremented only when the object is the base class, not the derived class.

    I know I could use the constructor overloading thing I learned in my last thread to accomplish this, but since it's a test project and the whole purpose is to learn new things, I thought I'd look into this to see if I could do it another way.

    If I have a way to say "if this object is of class object1", such as...
    Code:
    class object1
    {
      public:
      object1(void)
      {
        if(this->class == object1){id = object1_count++;}
      }
    };
    Obviously, I don't think I can do it like that, but it was just an example to show what type of ability I'm after.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    Well, you can decrement the object1_counter variable in each of the derived class's constructors:
    Code:
    class object2: public object1
    {
      public:
      object2(void)
      {
        type = OBJECT2; id = object2_count++;
        --object1_count;
      }
    };
    
    class object3: public object1
    {
      public:
      object3(void)
      {
        type = OBJECT3; id = object3_count++;
        --object1_count;
      }
    };
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    this is actually difficult enough to do when you take inheritance, copy constructors, etc into account. A simple way is to use a template like this
    Code:
    // A base class that provides counting
    template<class T> class Counted {
      static int count;
    public:
      Counted() {
        ++count;
      }
      Counted(const Counted<T>&) {
        ++count;
      }
      ~Counted() {
        --count;
      }
      static int getCount() {
        return count;
      }
    };
    
    template<class T> int Counted<T>::count = 0;
    
    // Curious class definitions
    class CountedClass : public Counted<CountedClass> {};
    class CountedClass2 : public Counted<CountedClass2> {};
    but to answer your original question
    Code:
    class object1
    {
      public:
      object1(void)
      {
        if(this->class == object1){id = object1_count++;}
      }
    };
    you can actually do this (well not that syntax, but that effect)
    look up RTTI, dynamic_cast and the typeid operator.

    Personnally I dislike RTTI. It's usually a symptom of bad design and there's almost always a better solution.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    40
    Took some time to find something that explained it well enough, but I did. I doubt I'll be able to remember that though, so I bookmarked it the explanation I found.

    Thanks much.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Inventory tracking of dynamically allocated items
    By Mario F. in forum C++ Programming
    Replies: 11
    Last Post: 07-23-2006, 05:39 PM
  3. Mmk, I give up, lets try your way. (Resource Management)
    By Shamino in forum Game Programming
    Replies: 31
    Last Post: 01-18-2006, 08:54 AM
  4. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 02:33 PM
  5. C++ Class Object Collection
    By Visual Develope in forum C++ Programming
    Replies: 3
    Last Post: 05-04-2002, 04:48 PM

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