Thread: How to check what class an object is?

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    23

    How to check what class an object is?

    I have been following some good tutorials, combined with advice from here, and have gotten pretty far. However, I can't seem to track down a bit of information(or I am mistaken and it can't be done in C++.) If I have the typical cheezy, lets say, Animal<-Dog<-Terrier kind of thing going, and I am passing around, say, a bunch of dogs, is there an equivalent to "Is this Dog a Terrier?", or even "Is this animal a Dog?" I suppose you could put a constant flag or something in each object to get around this, but I am wondering if it cannot be done more simply.

  2. #2
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    best method: most IDE's will give you object information simply by cursoring over the object.


    not so good method: hungarian notation
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    With proper design and good use of inheritance and virtual functions, you should not need to know exactly what type of object it is. That is, you should not have to make "special cases" and such, try building it into the design.

    That said, there is a way you can check, using dynamic_cast:
    Code:
    struct foo { ... };
    struct bar : public foo { ... };
    
    bool is_bar(const foo& obj)
    {
       bar* obj_b = dynamic_cast<bar*>(&foo);
       if(obj_b)
          return true;
       else
          return false;
    }
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  4. #4
    Registered User
    Join Date
    Jul 2005
    Posts
    23

    Ah ok

    So it's kind of like the "Why not use Goto" question: it simply won't be an issue if I make my objects properly

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    You also may (or may not) want to play around with this (provided you've enabled RTTI in your build):

    Code:
    #include <iostream>
    #include <typeinfo.h>
    using namespace std;
    
    template<typename T>
    void WhatType(T& obj)
    {
        const type_info& info = typeid(obj);
        cout << "You've passed an object of type " << info.name() << endl;
    }
    
    struct foo
    {
        int data;
    };
    
    int main()
    {
        float f;
        double d;
        foo bar;
    
        WhatType(f);
        WhatType(d);
        WhatType(bar);
    
        return 0;
    }
    Output:
    Code:
    You've passed an object of type float
    You've passed an object of type double
    You've passed an object of type struct foo
    "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

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    is there an equivalent to "Is this Dog a Terrier?", or even "Is this animal a Dog?"
    Why do you need to do that? Polymorphism allows you to do this:
    Code:
    #include <iostream>
    using namespace std;
    
    class Animal
    {
    public:
    	virtual void show()
    	{
    		cout<<"I am an Animal"<<endl;
    	}
    };
    
    class Dog : public Animal
    {
    public:
    	virtual void show()
    	{
    		cout<<"I am a Dog"<<endl;
    	}
    };
    
    class Terrier : public Dog
    {
    public:
    	virtual void show()
    	{
    		cout<<"I am a Terrier"<<endl;
    	}
    };
     
    int main()
    {
    	Animal A;
    	Dog D;
    	Terrier T;
    
    	Animal* p = &A;
    	p->show();
    
    	p = &D;
    	p->show();
    
    	p = &T;
    	p->show();
    
    
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Base-class pointer, accessing object from derived class
    By Korhedron in forum C++ Programming
    Replies: 15
    Last Post: 09-28-2008, 05:30 AM
  2. Replies: 8
    Last Post: 01-13-2008, 05:57 PM
  3. Is there a way to tell what class an object is?
    By Loduwijk in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2006, 09:20 PM
  4. class object manipulation
    By guda in forum C++ Programming
    Replies: 2
    Last Post: 10-09-2004, 10:43 AM
  5. C++ Class Object Collection
    By Visual Develope in forum C++ Programming
    Replies: 3
    Last Post: 05-04-2002, 04:48 PM