hamed
hamed

Reputation: 598

prevent changing access levels of inherited protected members

Is there a way to prevent someone from changing access level of inherited protected member? Someone can use a using declaration in derived class and increase access level of inherited protected member to public.

Now the question is how to prevent this?

example:

#include <iostream>
using namespace std;

class A {
 protected:
   int i(){};
 };

class B : public A {
 public:
   using A::i;
};

int main() {
  B b;
  b.i();
  return 0;
}

How to Prevent

#include <iostream>
using namespace std;

class A {
 private:
   void i(int) {};
 protected:
   int i(){};
 };

class B : public A {
 public:
   using A::i;
};

int main() {
  B b;
  b.i();
  return 0;
}

Upvotes: 2

Views: 151

Answers (1)

Thomas
Thomas

Reputation: 182093

You cannot. Even if you could prevent the using directive, derived classes can still simply expose a new public field that is a pointer or reference to the protected member, or a public member function that returns such a pointer or reference.

Upvotes: 3

Related Questions