If the base class has both const and non-const version of a function, can I refer to only one or the other in the derived class by the using keyword?
struct Base
{
protected:
int x = 1;
const int& getX() const {return x;}
int& getX() {return x;}
};
struct Derived : public Base
{
public:
using Base::getX; // Can we refer to the const or the non-const only?
};
If no, whats the simplest way to make const getX()
public in Derived without repeating the function body?
CodePudding user response:
using
always brings the entire overload set for the given name with it. You cannot invoke using
for a particular function, only for its name, which includes all uses of that name.
You will have to write a derived-class version of the function, with your preferred signature, that calls the base class. The simplest way would be with decltype(auto) getX() const { return Base::getX(); }
.