Specializing Subclassed Methods

C

cppaddict

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
 
B

Buster

cppaddict said:
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 ();
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top