By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,159 Members | 904 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,159 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Jun 29 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a

Philipp wrote:
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.

Jun 29 '06 #2

P: n/a
Philipp wrote:
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;
}
}

Jun 29 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.