Checking types.

This is a discussion on Checking types. within the C++ Programming forums, part of the General Programming Boards category; I have a C++ question, due to the fact that I'm using enheritance and a somewhat complex model, I need ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    97

    Checking types.

    I have a C++ question, due to the fact that I'm using enheritance and a somewhat complex model, I need to go through a list of objects and do a processo on only those who are a certain type. For example I have ENTITY_SOUND_LOCALIZED and ENTITY_SOUND_AMBIENT. I have the list of all entities but the problem is that I need to process in a function only the ENTITY_SOUND_LOCALIZED entities, so I don't how to check if an especific object is ENTITY_SOUND_LOCALIZED. I thought about using sizeof since the size would be different but it doesn't work, how do I check if an object is a certain type?

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    There are several potential solutions.

    You could make sure RTTI is enabled and use the typeid operator to determine the type.
    You could use a dynamic_cast on the object and if it succeeds then you have the right type.
    You could move all that processing into a virtual function that does nothing for some entities and does the correct work for the others.
    You could create a virtual function that simply returns true or false as to whether that entity type should be processed.

    I like the last option best, as it can be easily extended if you add new types - the base class would return false, and any new derived classes can return true if they should also be processed. If you used typeid or dynamic_cast, you would have to add the new type to that code whenever you needed it to be processed.

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Typically the way I cope with this is by having something like this:

    Example:
    Code:
    class BaseClass
    {
    public:
      enum TYPE {
          TYPE_A,
          TYPE_B
       };
    
       BaseClass(TYPE type) {
          m_type = type
       }
    
       TYPE getType() const {
          return m_type;
       }
    
    private:
       TYPE m_type;
    };
    
    class typeA : public BaseClass
    {
       classA() :
         BaseClase(BaseClass::TYPE_A)
       {
    
       }
    };
    
    class typeB : public BaseClass
    {
       classB() :
         BaseClase(BaseClass::TYPE_B)
       {
    
       }
    };

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    If its a certain type of problem you could use the visitor pattern. But a simple virtual method, which maybe empty, may be used in some cases.

    The visitor pattern works by having a visitor class, which implements different methods for different types:
    Code:
    class Visitor {
    public:
           virtual void visit(Car* car) = 0;
           virtual void visit(Truck* truck) = 0;
           virtual void visit(Motocycle* moto) = 0;
    };
    Then inside of the base class of objects you have an accept method which dispatches to the appropriate Visitor::visit method on its type:
    Code:
    class Basic_car {
    public:
            virtual void accept(Visitor* v) = 0;  /* all derived classes must dispatch on type */
    };
    
    class Car : public Basic_car {
    public:
          void accept(Visitor* v) { v->visit(this);  /* dispatch on Car */ }
    };
    
    class Truck : public Basic_car { 
    public:
          void accept(Visitor* v) { v->visit(this); /* dispatch on Truck */ }
    };
    
    class Motocycle : public Basic_car {
    public:
          void accept(Visitor* v) { v->visit(this); /* dispatch on Motocycle */ }
    };
    There must also be derived visitors. You could have variants of this idea: the point is that each derived class calls the appropriate method from visitor, selecting the method based upon its own type.

  5. #5
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Quote Originally Posted by master5001
    Typically the way I cope with this is by having something like this:

    Example:
    ...
    I've done the same, although there isn't much reason for keeping that variable in the class, it takes up space for every single instance.
    Code:
    class BaseClass
    {
    public:
        enum TYPE {
          TYPE_A,
          TYPE_B
        };
    
        // ...
        virtual TYPE getType() = 0;
    };
    
    class typeA : public BaseClass
    {
    public:
        // ...
        virtual TYPE getType() { return TYPE_A; }
    };
    
    class typeB : public BaseClass
    {
    public:
        // ...
        virtual TYPE getType() { return TYPE_B; }
    };
    Of course, you still must modify the base class every time you add a new derived class, which isn't very nice.

  6. #6
    Registered User
    Join Date
    Oct 2003
    Posts
    97
    Thanks for all the answers!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 10:38 AM
  2. The Interactive Animation - my first released C program
    By ulillillia in forum A Brief History of Cprogramming.com
    Replies: 48
    Last Post: 05-10-2007, 03:25 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  4. Problems about gcc installation
    By kevin_cat in forum Linux Programming
    Replies: 4
    Last Post: 08-09-2005, 10:05 AM
  5. Checking data types and errors
    By tegwin in forum C++ Programming
    Replies: 7
    Last Post: 03-12-2002, 10:42 PM

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