C++ quick tips: Overloaded virtual functions
Let’s suppose that you’re working with a virtual interface defined as follows:
|
|
Now, let’s have a class A
that implements this interface:
|
|
So far so good. Now, let’s say you want to add an overloaded version of
process
in class B. This would look like so:
|
|
An attempt to use this class’s original interface like so:
|
|
Will result in an unexpected compilation error:
|
|
If you add -Wall
to your compiler flags, you’ll see a warning:
|
|
This explains the problem. B::process(std::string)
is an overload which is
not a part of virtual interface. Its presence hides the original interface
declared in I
and implemented in A
.
To fix this, you need to bring the original interface into B
’s scope:
|
|
Now, B{}.process(123);
Will compile and work as expected. I’m surprised that the suggestion is not
a part of the warning message.