Thread: Type checking

  1. #1
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485

    Type checking

    I have two classes that are derived from a base class.

    Is there a way to check what type of class it is at runtime?

    Only thing I can come up with is having a virtual function that return a int and then have a global enum. But I assume that thats a bad solution.

    Thanks.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    typeinfo.
    However, it is better to try to specialize using templates.
    Tell us more about your problem.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    I have a need to add the total of one of the variables for each of the derived classes I have.

    I think this has something to do with dynamic_cast?
    Code:
    class Base
    {
        privat:
           int trackTime;
    }
    
    class raceCar : public Base
    {}
    
    class crapCar : public Base
    {}
    
    int main()
    {
        Base *carList[10];
    
        // Create some raceCars and crapCars
        .....
    
        int raceCarTotalTime = 0;
        int crapCarTotalTime = 0;
    
        for(carList)
        {
            if(carList[i] == raceCar)
                 raceCarTotalTime += carList[i]->getTime();
            else
                crapCarTotalTime += carList[i]->getTime();
        }
    }

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If I understood your problem right, then why not just some virtual functions?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    I was thinking of that.

    Something like:

    if(carList[i].getType() == "RaceCar)
    //Do this

    But though that there maybe was a better way of doing it?

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well no, if you need to get the time from each class, make getTime virtual. Then it will make sure you return the correct time for each class.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    Well no, if you need to get the time from each class, make getTime virtual. Then it will make sure you return the correct time for each class.
    But how can I then add it to two different variables depending on which class it belongs to?

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Something like:

    Code:
    class Base
    {
    public:
        virtual int getTime() = 0;
    };
    
    class A: public Base
    {
    private:
        int mytime;
    public:
        virtual int getTime() { return mytime; }
    };
    
    class B: public Base
    {
    private:
        int mytime;
    public:
        virtual int getTime() { return mytime; }
    };
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    Yes, I know how to create virtual functions. But when using virtual functions like you posted, I have no way of knowing of the time variable comes from a raceCar object or a crapCar object

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    But that's the point, isn't it? That each class keeps its own variable and returns it. What more do you need to know?
    It's better to place code that relies on specific classes INTO those specifies classes, and doing the work via virtual functions. Then you don't have to worry about what type of object it is.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    Say I have a racing game where there are two teams. Team one are all using raceCar objects, team two are crapCars.

    The two classes have all the normal move(), draw() and so one. They also have a variable to see how long the car has been on the track.

    At the end of the game, I want to add up the total time all the raceCars have been on the track, and the total time all the crapCars have been on the track to see who was on the track the most. In order to add in into two different variables, I need to know which class the variable comes from.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Aha.
    Well, I'd use an enum for that. It's better than dynamically querying for the type using type_info.
    You should know how to utilize it.

    Another way of doing it is having two vectors perhaps, one with each type of car. The you can search through each vector and know which type it is for free.
    Remember that if you use pointers, you have many references to the same object.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by h3ro View Post
    Say I have a racing game where there are two teams. Team one are all using raceCar objects, team two are crapCars.

    The two classes have all the normal move(), draw() and so one. They also have a variable to see how long the car has been on the track.

    At the end of the game, I want to add up the total time all the raceCars have been on the track, and the total time all the crapCars have been on the track to see who was on the track the most. In order to add in into two different variables, I need to know which class the variable comes from.
    What if each team has both raceCars and crapCars? Or raceCars, crapCars, and the newly created superCars and bonzoCars? Your code will become a hive of switch statements and type interrogation. Blech.

    Just make each team manage a collection of base car pointers and use virtual functions to total the time, move, draw, etc.

  14. #14
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    Just make each team manage a collection of base car pointers and use virtual functions to total the time, move, draw, etc.
    I cant. They have to be in the same array.

    What if each team has both raceCars and crapCars? Or raceCars, crapCars, and the newly created superCars and bonzoCars? Your code will become a hive of switch statements and type interrogation. Blech.
    There will never be a mix between then and never more then two car types. The project is done, adding up scores at the end is the only part that is left

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    They have to be in the same array.
    You could use the same array for "a collection of base car pointers".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 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
  2. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 04:33 PM
  3. Data type checking?
    By Trauts in forum C++ Programming
    Replies: 2
    Last Post: 02-09-2003, 05:24 PM
  4. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM
  5. Type checking
    By Araenor in forum C Programming
    Replies: 10
    Last Post: 08-29-2001, 12:47 AM