Thread: How do you check object's type.

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    4

    How do you check object's type.

    In implementing a Radiosity renderer I am iterating over a list of objects with the following:

    Code:
    for (Quad* o = scn.obj; o; o=o->next)
    Quad is a subclass of GeomObj. Since the scene I am rendering should only be comprised of Quads, this code should not crash in the context I need it, however it throws a warning during compilation. I'm just wondering how check the type of the object 'o' so I can just skip over any GeomObjs if they don't happen to be Quads.

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    You cannot check types directly. You could use some kind of variable that is inherited:
    Code:
    class GeomObj
    {
       ...
    
       bool IsQuad;
    }
    
    class Quad : public GeomObj
    {
       public:
          Quad()
          {
             IsQuad = true;
          }
    }
    
    class NonQuadOrWhatever : public GeomObj
    {
       public:
          NonQuadOrWhatever()
          {
             IsQuad = false;
          }
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    You cannot check types directly.
    What about typeid():

    http://msdn.microsoft.com/library/de...d_operator.asp

    I can't get it to work--not even MS's simple example--but according to the description, it seems like it should work. This is the error I get:

    warning C4541: 'typeid' used on polymorphic type 'class Base' with /GR-; unpredictable behavior may result

    ...which I thought was the whole point.
    Last edited by 7stud; 03-04-2005 at 04:06 PM.

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    magos, you actually can check types, with some restrictions though. By using typeid you can compare the type of two objects that have a virtual method.


    tejj, you could use rtii. But you might also want to consider something like this.

    Code:
    class GeomVisitor {
    public:
           virtual void visit(Quad* quad) = 0;
           virtual void visit(GeoObj* geomObj) = 0;
    };
    
    // ... //
    
    class ScreenRender : public GeomVisitor {
    public:
             // ... //
             virtual void visit(Quad* quad);
             virtual void visit(GeoObj* geoObj);
    };
    
    class GeomObj {
    public:
             virtual void accept(GeomVisitor* v) {
                    v->visit(this);
             }
    };
    
    class Quad : public GeomObject {
    public:
          virtual void accept(GeomVisitor* v) {
                 v->visit(this);
          }
    };
    
    // ... //
    ScreenRender render;
    
    for (Quad* o = scn.obj; o; o=o->next) {
          o->accept(&render);      
    }
    You'd then pay the price of two virtual calls, comparative to the one virtual call and one comparison you'd have using typeid(which requires accessing the typeid/virtual table anyhow).

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    7stud, when using MSVC++ the rtti can be turned on in the project properties->C++->language page
    Last edited by okinrus; 03-04-2005 at 04:23 PM.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    What the heck is rtti? And, why should a standard language feature depend on a compiler setting?

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Ok, after looking in VC6 help, I found out what rtti is: run time type information. Is it off by default because it slows things down?

    Here is the example I was envisioning:
    Code:
    #include <iostream>
    #include <typeinfo>
    
    using namespace std;
    
    class GeomObj
    {
    public:
       virtual void display()
       {
    	   cout<<"I am type GeomObj."<<endl;
       }
    };
    
    class Quad : public GeomObj
    {
    public:
    	virtual void display()
    	{
    		cout<<"I am type Quad."<<endl;
    	}
    };
    
    class NotQuad : public GeomObj
    {
    public:
    	virtual void display()
    	{
    		cout<<"I am type NotQuad."<<endl;
    	}
    };
    
    
    
    int main()
    {
    	GeomObj* array[2];
    	array[0] = &Quad();
    	array[1] = &NotQuad();
    
    	for(int i=0; i<2; i++)
    	{
    		if(typeid(*array[i])==typeid(Quad))
    		{
    			array[i]->display();
    		}
    	}
       
    
       return 0;
    }
    Last edited by 7stud; 03-04-2005 at 06:13 PM.

  8. #8
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    There's some storage cost involved with using rtti. For each class, it must store the name of a class and the other data inside the type info. The virtual table, too, must have a pointer to the type info.

  9. #9
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    RTTI is extremely useful for debugging purposes and is useful for programs when you really must know the type of object - much of this is generic programming but RTTI enables you to check the actual type even if it is a user-defined class. Very handy at times.

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. HELP!!!!emergency Problem~expert please help
    By unknowppl in forum C++ Programming
    Replies: 9
    Last Post: 08-21-2008, 06:41 PM
  3. HELP!!!!emergency ~expert please help
    By unknowppl in forum C Programming
    Replies: 1
    Last Post: 08-19-2008, 07:35 AM
  4. Can you check what is wrong with this code
    By Ron in forum C++ Programming
    Replies: 4
    Last Post: 08-01-2008, 10:59 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM