I want to do make a public member in a base class private in a derived class, like this:
class A {
public:
int x;
int y;
};
class B : public A {
// x is still public
private:
// y is now private
using y;
};
But apparently “using” can’t be used that way. Is there any way to do this in C++?
(I can’t use private inheritance because there are other members and functions of A that must still be public.)
Short answer: no. Liskov substitution and the nature of public inheritance demands that everything that you can do with an
A(i.e. its public members) can also be done byB. That means you can’t hide a public method.If you’re trying to hide public
fields, there isn’t much you can do. To “hide” public methods, you could do something like: