Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Calling class specific methods on a superclass object (or - Is therea getClass()?)

Reply
Thread Tools

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

 
 
Philipp
Guest
Posts: n/a
 
      06-29-2006
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
 
Reply With Quote
 
 
 
 
Vikram
Guest
Posts: n/a
 
      06-29-2006

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.

 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      06-29-2006
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;
}
}

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Open Source: There’s A Lot Of It About Lawrence D'Oliveiro NZ Computing 3 07-26-2010 01:42 AM
calling class methods from class methods, help? Oltmans Python 6 03-11-2009 07:59 PM
How to check if another object is my superclass from a function in aneven higher superclass? bart van deenen C++ 6 03-03-2009 02:44 PM
Is there a way to find the class methods of a class, just like'methods' finds the instance methods? Kenneth McDonald Ruby 5 09-26-2008 03:09 PM
calling superclass __init__ when superclass is object Evan Klitzke Python 0 08-02-2007 05:15 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57