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

Discussion in 'C++' started by Philipp, Jun 29, 2006.

  1. Philipp

    Philipp Guest

    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
    Philipp, Jun 29, 2006
    #1
    1. Advertising

  2. Philipp

    Vikram Guest

    Re: Calling class specific methods on a superclass object (or - Is there a getClass()?)

    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.
    Vikram, Jun 29, 2006
    #2
    1. Advertising

  3. Philipp

    Rolf Magnus Guest

    Re: Calling class specific methods on a superclass object (or - Is there a getClass()?)

    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;
    }
    }
    Rolf Magnus, Jun 29, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    6
    Views:
    9,373
    Viator
    Nov 24, 2005
  2. Replies:
    2
    Views:
    7,902
  3. odwl
    Replies:
    1
    Views:
    12,207
    Thomas Hawtin
    Jun 15, 2006
  4. Evan Klitzke
    Replies:
    0
    Views:
    362
    Evan Klitzke
    Aug 2, 2007
  5. bart van deenen
    Replies:
    6
    Views:
    753
    bart van deenen
    Mar 3, 2009
Loading...

Share This Page