My class Dad
provides some protected
methods, that its children will need, even if it doesn't actually know yet who these children will be.
class Dad
{
protected:
void method()
{
// some amazing stuff (I swear)
};
};
The actual inheriting class Child: public Dad
, in the current implementation of my program, has decided to be derived itself into several classes class GrandKid1: Child
, class GrandKid2: Child
etc.
But, for the sake of safety and organisation, Child
prefers the grandkids not to be able to call the method()
by themselves. How do I prevent them from doing this?
Obviously, the following naive code yields a linker error:
class Child: public Dad
{
private:
void method();
};
How do I make Child
stop the propagation of the protected
member method()
to its own derived classes?
You can use using
directive to put member into another section.
class Dad
{
protected:
void Method() { std::cout << "Dad"; }
};
class Child : public Dad
{
private:
using Dad::Method;
};
class GrandChild : public Child
{
public:
void f1() { Method(); } // Generates compilation error
};