Access to the declaration of an overloaded base class method

Given that we have overloaded methods in the base class and a derived class that was inherited as private / protected.

  • Is it possible to restore only one / several initial access levels of overloaded methods?
  • In GCC 4.4.0, I try to use the basic methods under secure access, and then inherit it using private access. When I try to restore public access level, it works! Is this supposed to work? or is it a compiler error? As far as I understand, access level recovery should not be used to advance or lower the level of access to a member.

Code snippet:

class base { public: void method() {} void method(int x) {} protected: void method2() {} }; class derived : private base { public: base::method; // Here, i want to restore only the none parameterized method base::method2; // method2 is now public?? }; 
+6
c ++ overloading
source share
3 answers

Changing the accessibility of inherited functions using a declaration cannot be done selectively for a given overload for the simple reason that a using declaration introduces a name into a declarative region and that, by definition, function overloads have the same name.

The only alternative I see here is to use trivial forwarding functions:

 class derived : private base { public: void method() { base::method(); } using base::method2; // method2 is now public // method(int) stays inaccessible }; 

I'm not quite sure I understand your second question, but yes: you can change the accessibility of base elements in a derived class through using declarations.

+4
source share

You are not restoring access as such. You have set access. As you do above, you can explicitly set access for any method, including one previously declared private .

+1
source share

It would be impossible to prevent protected methods from being publicly accessible if the derived class wanted this, as you could just write a small wrapper and do it. private is another matter.

+1
source share

All Articles