va.
va.

Reputation: 780

C++ cli pure virtual function overloading (or disabling?)

I don't know if it is possible, but there is simple example what I should achieve: I have to use the first pure virtual function, because there are a lot of derived classes that needs it. But a few derived classes needs the second parameter too.

There is the base class Car:

public ref class Car abstract
{
.....
public:
  virtual void move(Road ^ road) = 0; //I am not allowed to delete this line
  //virtual void move(Road ^ road, Parameter2 ^ parameter2) = 0; //overload
}

Is it possible to overload the pure virtual function? Or in the worst case to disable that function in that few classes which need two parameters?

I am just learning, sorry for stupid questions..

Upvotes: 1

Views: 1316

Answers (1)

Attila
Attila

Reputation: 28762

It is certaily possible to overload a virtual function. Which one is called is based on the number (and type) of the function parameters. Note that pure virtual functions ('=0') impose the requirement on all derived classes to implement all pure virtual functions of the base class (or be abstract themselves as well, passing down the requirement to further derived classes).

If you can modify the signature/body of the first declaration, you can get away with only one function, though:

  virtual void move(Road ^ road, Parameter2 ^ parameter2 = 0) = 0;

which assigns a default parameter to parameter2, when none is supplied explicitly at the time of calling. Then have the logic be identical to your first case (no parameter2), when the value of parameter2 is 0.

Upvotes: 2

Related Questions