Calling class specific methods on a superclass object (or - Is therea getClass()?)

P

Philipp

Hello
This seems a simple question, please send me to the right FAQ if I
missed it:

If there is a class Fruits with two classes Apples and Bananas extending
Fruits. Apples has a method called isRed() which returns true if the
apple is red (Bananas don't have such a method)

Is it somehow possible to make code like this:
-- pseudo-code --

aMethod(Fruit* aFruit){
if (aFruit->getClass() == Apples) {
cout << "Is the apple red?:" << aFruit->isRed() << endl;
}
}

Does such a thing as getClass() exist (by default)?
Can I cast aFruit to an Apple pointer and test if the cast is valid?
Is there another nice (and light) way to implement it (except
implementing getClass() in Fruits and then in each child class)?

Thanks for your answers.
Philipp
 
V

Vikram

Philipp said:
Hello
This seems a simple question, please send me to the right FAQ if I
missed it:

If there is a class Fruits with two classes Apples and Bananas extending
Fruits. Apples has a method called isRed() which returns true if the
apple is red (Bananas don't have such a method)

Is it somehow possible to make code like this:
-- pseudo-code --

aMethod(Fruit* aFruit){
if (aFruit->getClass() == Apples) {
cout << "Is the apple red?:" << aFruit->isRed() << endl;
}
}

Does such a thing as getClass() exist (by default)?
Can I cast aFruit to an Apple pointer and test if the cast is valid?
Is there another nice (and light) way to implement it (except
implementing getClass() in Fruits and then in each child class)?

Thanks for your answers.
Philipp

dynamic_cast is what you are looking for. It must be in the FAQ or you
can just use google.
 
R

Rolf Magnus

Philipp said:
Hello
This seems a simple question, please send me to the right FAQ if I
missed it:

If there is a class Fruits with two classes Apples and Bananas extending
Fruits. Apples has a method called isRed() which returns true if the
apple is red (Bananas don't have such a method)

Is it somehow possible to make code like this:
-- pseudo-code --

aMethod(Fruit* aFruit){
if (aFruit->getClass() == Apples) {
cout << "Is the apple red?:" << aFruit->isRed() << endl;
}
}

You should avoid such situations wherever possible.
Does such a thing as getClass() exist (by default)?

Well, there is typeinfo or dynamic_cast.
Can I cast aFruit to an Apple pointer and test if the cast is valid?
Yes.

Is there another nice (and light) way to implement it (except
implementing getClass() in Fruits and then in each child class)?

aMethod(Fruit* aFruit)
{
Apple* apple = dynamic_cast<Apple*>(aFruit);
if (apple)
{
cout << "Is the apple red?:" << apple->isRed() << endl;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top