Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access Declaration of Base Class Overloaded Method

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

  1. Can we restore only one/several of the original access level of the overloaded methods?
  2. On GCC 4.4.0 i try to put the base methods under protected access, then inherited it using private access. When i try to restore the access level to public, it works! Is this how its suppose to work? or is it a bug on the compiler? To my understanding, restoring access level shouldn't be able to be used to promote or demote a member's access level.

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??
};
like image 591
Ignatius Reza Avatar asked Jan 05 '11 17:01

Ignatius Reza


2 Answers

Changing accessibility of inherited functions through a using declaration cannot be done selectively on given overload for the simple reason that a using declaration only introduces a name into the declarative region and that by definition, functions overloads share 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 base members accessibility in a derived class through using declarations.

like image 99
icecrime Avatar answered Oct 13 '22 12:10

icecrime


You don't restore access, per se. You set access. As you are doing above, you can explicitly set the access for any method, including ones previously declared as private.

like image 22
John Dibling Avatar answered Oct 13 '22 11:10

John Dibling