Specializing Subclassed Methods

Discussion in 'C++' started by cppaddict, Jun 3, 2004.

  1. cppaddict

    cppaddict Guest

    Hi,

    How can you invoke a superclass's method from within the subclass
    method that is overriding it?

    For example, say that in the Car class we have a method like:

    Car::turnOn() {
    startEngine();
    illuminateDashboard()
    //other stuff
    }

    Now we want to specialize this method for a Corvette, which also flips
    up its headlights when turned on. I'd like to do:

    Corvette::turnOn() {
    flipUpHeadlights();
    super.turnOn()
    }

    But I know C++ does not support the super keyword like that. What is
    the correct way to handle this situation?

    Thanks,
    cpp
     
    cppaddict, Jun 3, 2004
    #1
    1. Advertising

  2. cppaddict

    Buster Guest

    cppaddict wrote:

    > Hi,
    >
    > How can you invoke a superclass's method from within the subclass
    > method that is overriding it?
    >
    > For example, say that in the Car class we have a method like:
    >
    > Car::turnOn() {
    > startEngine();
    > illuminateDashboard()
    > //other stuff
    > }
    >
    > Now we want to specialize this method for a Corvette, which also flips
    > up its headlights when turned on. I'd like to do:
    >
    > Corvette::turnOn() {
    > flipUpHeadlights();
    > super.turnOn()
    > }
    >
    > But I know C++ does not support the super keyword like that. What is
    > the correct way to handle this situation?


    Car::turnOn ();

    // or

    this->Car::turnOn ();

    // or you can put

    class Corvette : public Car
    {
    public:
    typedef Car super;
    // ...
    };

    // and use

    super::turnOn ();

    --
    Regards,
    Buster.
     
    Buster, Jun 3, 2004
    #2
    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. =?Utf-8?B?U2xpbWVtYW4=?=

    Cannot view subclassed Page in Visual Designer

    =?Utf-8?B?U2xpbWVtYW4=?=, Jul 15, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    484
    =?Utf-8?B?U2xpbWVtYW4=?=
    Jul 20, 2005
  2. -
    Replies:
    3
    Views:
    464
    Thomas Fritsch
    Jul 24, 2005
  3. Benji
    Replies:
    1
    Views:
    461
    Chris Uppal
    Oct 6, 2005
  4. Replies:
    20
    Views:
    2,213
  5. Replies:
    3
    Views:
    151
    Robert Klemme
    Oct 9, 2008
Loading...

Share This Page