I have a simple C++ base class, derived class example.
// Base.hpp
#pragma once
class Base
{
public:
virtual float getData();
virtual void setData(
A function in a derived class will hide any function of the same name in a base class.
If you really want the base class function(s) of that name to be visible, you can add a using declaration to make it happen:
class Base {
public:
void setData(float d);
void setData(float d, float e);
};
class Derived : public Base {
using Base::setData;
void SetData(float d);
};
[Revised:] Seems my memory was wrong. This doesn't cause an ambiguity -- even with the using declaration, Derived::setData
takes precedence (so to speak) so when you invoke setData(float)
on a derived object, you get Derived::setData(float)
. If you make it virtual and invoke via a pointer to Base, you still get Derived::setData(float);
.
This is an artifact of C++ name lookup. The basic algorithm is the compiler will start at the type of the current value and proceed up the hierarchy until it finds a member on the type which has the target name. It will then do overload resolution on only the members of that type with the given name. It does not consider members of the same name on parent types.
The way to work around this is to redefine the functions on Derived
and just forward them up to Base
class Derived {
...
void setData(float a, float b);
}
void Derived::setData(float a, float b) {
Base::setData(a,b);
}
Additionally you can bring the base members into scope using the using
declaration
class Derived {
using Base::setData;
...
}
Documentation on this use of using