B
Big Blue Ox
Hello,
I am trying to store different types that are all derived from the same
base class in a list template.
So for instance lets say I have this is fragment of skeleton code:
class Pet
{
string name;
int feeding_times;
};
class Dog: public Pet
{
int play_time;
};
class Cat: public Pet
{
int kitty_litter_type;
};
class Fish: public Pet
{
int water_temp;
};
class Kennel
{
list<Pet> bordered_pets;
public:
void addPet(Pet& pet)
{
bordered_pets.push_back(pet);
}
};
main()
{
Dog german_shepard;
Cat callico;
Dog dalmation;
Kennel kennel;
kennel.addPet(german_shepard);
kennel.addPet(callico);
kennel.addPet(dalmation);
}
Now, if I want iterate through the list to check whether or not the
kennel list contains a class of type fish or not, how do I do this? Or
I guess a more general question is, is it legal to have a list of
different generic class types and if so how do I tell one apart from
another.
BBO
I am trying to store different types that are all derived from the same
base class in a list template.
So for instance lets say I have this is fragment of skeleton code:
class Pet
{
string name;
int feeding_times;
};
class Dog: public Pet
{
int play_time;
};
class Cat: public Pet
{
int kitty_litter_type;
};
class Fish: public Pet
{
int water_temp;
};
class Kennel
{
list<Pet> bordered_pets;
public:
void addPet(Pet& pet)
{
bordered_pets.push_back(pet);
}
};
main()
{
Dog german_shepard;
Cat callico;
Dog dalmation;
Kennel kennel;
kennel.addPet(german_shepard);
kennel.addPet(callico);
kennel.addPet(dalmation);
}
Now, if I want iterate through the list to check whether or not the
kennel list contains a class of type fish or not, how do I do this? Or
I guess a more general question is, is it legal to have a list of
different generic class types and if so how do I tell one apart from
another.
BBO