Overload a virtual method in a derived class so it takes more parameters in C++

First and foremost I want to apologize if this is a duplicate, it does sound like a question that has been asked a million times but I can’t seem to find an answer that suits me.

I have an abstract class A with the pure virtual method void foo(int a) = 0

Then I have several classes that inherit from A and all of them define the method foo. But I need one of them, B, to make it so foo takes an extra parameter, int b, so sort of an overload.

Then I would like to do this:

A *bInstance = new B();
bInstance -> foo(1, 2);

But I get an error telling me that foo is taking too many parameters.

Writing this, I realize it’s kind of a weird thing to do so maybe you can’t do this and it’s good that you can’t do it. But in case it is possible, please do tell mw how I should go about it. Thanks

Source: Windows Questions C++

LEAVE A COMMENT