Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hej :)

How can i detect the class type of a specific object in c++ or What is the best way to find out which type the object is on my array ?
For example what if i want to set a new age for all the cats only without knowing there positions(index) in the array.

And please write the if-statement or whatever so i can understand it better.

Thank you!

What I have tried:

//Base class
class Animal
{
private:
std::string name;
int age ;

public:
Animal(std::string name = "Unknown" , int age =0);
~Animal();
};

//derived class
class Dog : public Animal
{
public:
Dog(std::string name = "Unknown" , int age =0);
~Dog();
void setAge (int newAge) const;
};

//derived class
class Cat: public Animal
{
public:
Cat(std::string name = "Unknown" , int age =0);
~Cat();
void setAge (int newAge) const;
};


//in main.cpp

Animal *arr[5];

arr[i] = new Cat("cat1",11);
arr[i] = new Dog("dog1",12);
arr[i] = new Dog("dog2",13);
arr[i] = new Dog("dog3",14);
arr[i] = new Cat("cat2",15);
Posted
Updated 13-Jan-17 1:55am

Have also a look at typeid operator[^].
 
Share this answer
 
Comments
Richard MacCutchan 13-Jan-17 9:32am    
+5 - easy when you know how.
You have a couple of alternatives:

(a) Adding you own type field to the base class
C++
class Animal
{
public:
   int m_type;

   ...
}

That is not very object oriented and would be my last resort.

(b) Use the run-time type information system of C++:
C++
Cat* pCat = dynamic_cast<Cat*> (arr[i]);
if (pCat != NULL)
   pCat->age = 14;

Don't forget that run-time type information is only available for polymorphic classes, i.e. classes that have at least one virtual function. That is no problem in your case. In fact, you should make the destructor of your base class virtual, so you can delete your array elements properly!

This alternative is a little better than (a) in terms of object orientation, but still kind of a last resort for some existing code that was not properly designed.

(c) Create a virtual function that does whatever you want to do. For example, say you want to adjust all animal ages by some formula that is animal type specific. You could implement a virtual function like:
C++
class Animal
{
public:
   virtual void AdjustAge () = 0;

...
}

class Cat
{
public:
   virtual void AdjustAge ()
   {
      age = age * CAT_SPECIFIC_FACTOR;
   }
...
}

class Dog
{
public:
   virtual void AdjustAge ()
   {
      age = age * DOG_SPECIFIC_FACTOR + 5;
   }
...
}

Then in the calling code you would just do:
C++
for (i = 0; i < ....)
     arr[i]->AdjustAge();

This would be the most object oriented solution. Making AdjustAge an abstract virtual function (the = 0;) makes sure that you don't forget to implement the function in each derived class. This maintenance friendly and easy to figure out for someone who has to maintain your code later on.
 
Share this answer
 
Comments
Albert Holguin 13-Jan-17 9:19am    
Virtual method would be the approach I'd use for this type of problem. +5 for list of approaches to solving the problem...
C++ does not have any mechanism for detecting class types at runtime. You would need to add some extra properties to your classes. Alternatively there are some third-party libraries that may help; Google for "C++ reflection".

Thanks to Carlo, I now know the correct answer.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900