473,399 Members | 2,278 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Runtime Type Checking

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

Feb 1 '06 #1
5 2423
Big Blue Ox wrote:
I am trying to store different types that are all derived from the same
base class in a list template.
You cannot store anything in a template. You can only store it in a class
object. It has to be a concrete class, so if you have a template, you got
to have an instantiation of it. Just so we use proper terminology.
So for instance lets say I have this is fragment of skeleton code:

The following is redacted a bit.
class Pet {};
class Dog: public Pet {};
class Cat: public Pet {};
class Fish: public Pet {};

class Kennel
{
list<Pet> bordered_pets;
public:
void addPet(Pet& pet) { bordered_pets.push_back(pet); }
};

main()
int 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?
You don't. As soon as you [attempt to] store an object of a derived class
in a standard container instantiated on the base class, you get _slicing_.
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.


Google for "heterogeneous container".

V
--
Please remove capital As from my address when replying by mail
Feb 1 '06 #2

Big Blue Ox wrote:
class Kennel
{
list<Pet> bordered_pets;

public:

void addPet(Pet& pet)
{
bordered_pets.push_back(pet);
}
};
inserting an item into a container copies that item. Your
bordered_pets list contains Pets, it does not contain Cats and Dogs.
You can fix this by having a list of pointers to Pet. You will likely
want a clone() method in your polymorphic types. There are many
different ownership strategies and you will need to pick one. The easy
way is probably to use boost's smart_ptr.
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.


Ways to detect your type:
a) compare type_info.
b) attempt a dynamic cast
c) Pets has a "type" field.

Others may have others.

Feb 1 '06 #3

"Big Blue Ox" <ec****************@yahoo.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
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


The following compiles and runs. Notice: I used vector instead of list just
cause it was easier for me.

#include <iostream>
#include <string>
#include <vector>

class Pet
{
public:
virtual ~Pet() {};
private:
std::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
{
public:
std::vector<Pet*> bordered_pets;
void addPet(Pet* pet)
{
bordered_pets.push_back(pet);
}
void whatPet( int index )
{
if ( index >= bordered_pets.size() )
std::cout << "Bad Index" << std::endl;
else if ( typeid( *bordered_pets[index] ) == typeid( Dog ) )
std::cout << "it's a dog!" << std::endl;
else if ( typeid( *bordered_pets[index] ) == typeid( Cat ) )
std::cout << "It's a cat!" << std::endl;
else if ( typeid( *bordered_pets[index] ) == typeid( Fish ) )
std::cout << "It's a fish!" << std::endl;
else
std::cout << "I don't know what the heck this is!" << std::endl;
}
};

int main()
{
Dog* german_shepard = new Dog;
Cat* callico = new Cat;
Dog* dalmation = new Dog;
Fish* goldfish = new Fish;
Pet* catfish = new Fish;

Kennel kennel;

kennel.addPet(german_shepard);
kennel.addPet(callico);
kennel.addPet(dalmation);
kennel.addPet(goldfish);
kennel.addPet(catfish);

for ( int i = 0; i < 5; ++i )
kennel.whatPet( i );

std::string wait;
std::getline( std::cin, wait );
}
Feb 2 '06 #4
> The following compiles and runs. Notice: I used vector instead of list
just cause it was easier for me.

#include <iostream>
#include <string>
#include <vector>

class Pet
{
public:
virtual ~Pet() {};
private:
std::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
{
public:
std::vector<Pet*> bordered_pets;
void addPet(Pet* pet)
{
bordered_pets.push_back(pet);
}
void whatPet( int index )
{
if ( index >= bordered_pets.size() )
std::cout << "Bad Index" << std::endl;
else if ( typeid( *bordered_pets[index] ) == typeid( Dog ) )
std::cout << "it's a dog!" << std::endl;
else if ( typeid( *bordered_pets[index] ) == typeid( Cat ) )
std::cout << "It's a cat!" << std::endl;
else if ( typeid( *bordered_pets[index] ) == typeid( Fish ) )
std::cout << "It's a fish!" << std::endl;
else
std::cout << "I don't know what the heck this is!" << std::endl;
}
};

int main()
{
Dog* german_shepard = new Dog;
Cat* callico = new Cat;
Dog* dalmation = new Dog;
Fish* goldfish = new Fish;
Pet* catfish = new Fish;

Kennel kennel;

kennel.addPet(german_shepard);
kennel.addPet(callico);
kennel.addPet(dalmation);
kennel.addPet(goldfish);
kennel.addPet(catfish);

for ( int i = 0; i < 5; ++i )
kennel.whatPet( i );

std::string wait;
std::getline( std::cin, wait );
}


Ooops, I used new and I didn't use delete. This fixes that.
#include <iostream>
#include <string>
#include <vector>

class Pet
{
public:
virtual ~Pet() {};
private:
std::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
{
public:
std::vector<Pet*> bordered_pets;
void addPet(Pet* pet)
{
bordered_pets.push_back(pet);
}
void whatPet( int index )
{
if ( index >= bordered_pets.size() )
std::cout << "Bad Index" << std::endl;
else if ( typeid( *bordered_pets[index] ) == typeid( Dog ) )
std::cout << "it's a dog!" << std::endl;
else if ( typeid( *bordered_pets[index] ) == typeid( Cat ) )
std::cout << "It's a cat!" << std::endl;
else if ( typeid( *bordered_pets[index] ) == typeid( Fish ) )
std::cout << "It's a fish!" << std::endl;
else
std::cout << "I don't know what the heck this is!" << std::endl;
}
~Kennel()
{
for ( std::vector<Pet*>::iterator it = bordered_pets.begin(); it !=
bordered_pets.end(); ++it )
delete *it;
}

};

int main()
{
Dog* german_shepard = new Dog;
Cat* callico = new Cat;
Dog* dalmation = new Dog;
Fish* goldfish = new Fish;
Pet* catfish = new Fish;

Kennel kennel;

kennel.addPet(german_shepard);
kennel.addPet(callico);
kennel.addPet(dalmation);
kennel.addPet(goldfish);
kennel.addPet(catfish);

for ( int i = 0; i < 5; ++i )
kennel.whatPet( i );

std::string wait;
std::getline( std::cin, wait );
}
Feb 2 '06 #5
Thanks for all the suggestions! You have all been very helpful to me. I
also just realized that this topic is covered extensively in
Stroustrup's Book; C++ Programming language... section 12.2.5 if anyone
else is interested.

Thanks again,
BBO

Feb 2 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: David Whitney | last post by:
All: I've been working with some Reflection code to interrogate some runtime object information, and was wondering the following... Is there any call that can be made to determine whether a...
9
by: Jon Rea | last post by:
I hav been looking for the last 2 hours on how to do this without much luck. Im going to give a simplifed model of the problem i have. I want a collection class that can holds a series or...
5
by: John Hardin | last post by:
All: Is it possible at runtime to determine the names of all classes derived from a given class? -- John Hardin KA7OHZ <johnh@aproposretail.com> Internal Systems...
5
by: Jon Lapham | last post by:
I have been using the EXPLAIN ANALYZE command to debug some performance bottlenecks in my database. In doing so, I have found an oddity (to me anyway). The "19ms" total runtime reported below...
7
by: Martin Robins | last post by:
I am currently looking to be able to read information from Active Directory into a data warehouse using a C# solution. I have been able to access the active directory, and I have been able to return...
17
by: marcus | last post by:
Hi, I'm trying to get a C# application developed with .NET 2.0 tools to run with .NET 1.1. runtime. Is this possible? I have only used methods and properties compatible on .NET 1.1. Are there...
10
by: Rich | last post by:
I want to replace CSomeObject class with some kind of runtime method that returns type CSomeObject that I can use as cast. How do I specify type of explicit cast at runtime? eg: object...
7
by: Charlie Brookhart | last post by:
I have a program (posted below) that is supposed to take liters, which is the user input, and convert it to pints and gallons. The pints and gallons are displayed in a read only textbox. I don't...
3
by: Dave G | last post by:
I recently finished a system in Access 2003 for a client and loaded it onto their server. They use the A2003 runtime version. One of the main forms contains a TAB control. On moving away from one...
1
by: coltsith | last post by:
Any suggestions on how I can check the keyboard type during runtime? Any p/invoke methods or dlls? I'm using Windows Mobile 5, and .Net Compact Framework 2.0. Thanks a lot
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.