Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access to base class function

class Base
{
    public: void foo(){}
};

class Derived : public Base
{
    private:
    using Base::foo;
};

int main()
{
   Derived d;
   d.foo();
}

Is the code legal? The declaration using Base::foo is in the private section of the derived class. So the call d.foo() shouldn't compile, am I right?

like image 247
JKS Avatar asked Nov 29 '10 16:11

JKS


People also ask

How do you access the base class method?

The base keyword is used to access members of the base class from within a derived class: Call a method on the base class that has been overridden by another method. Specify which base-class constructor should be called when creating instances of the derived class.

What is the function of base class?

Base Class: A base class is a class in Object-Oriented Programming language, from which other classes are derived. The class which inherits the base class has all members of a base class as well as can also have some additional properties.

How can private members access base class?

Private members of the base class cannot be used by the derived class unless friend declarations within the base class explicitly grant access to them. In the following example, class D is derived publicly from class B . Class B is declared a public base class by this declaration.


2 Answers

Right.

Now reality check…

MinGW g++ 4.4.1:

x.cpp: In function 'int main()': x.cpp:3: error: 'void Base::foo()' is inaccessible
x.cpp:15: error: within this context

Visual C++ 10.0:

x.cpp(15) : error C2248: 'Derived::foo' : cannot access private member declared in class 'Derived'
x.cpp(9) : see declaration of 'Derived::foo'
x.cpp(6) : see declaration of 'Derived'

Comeau Online 4.3.10.1:

In strict mode, with -tused, Compile succeeded (but remember, the Comeau online compiler does not link).
Compiled with C++0x extensions enabled.

Oops. And Comeau is the one that's nearly always right! Well, turning off C++0x extensions, for C++98/C++03:

In strict mode, with -tused, Compile succeeded (but remember, the Comeau online compiler does not link).
Compiled with C++0x extensions DISabled.

Oops!

Well, you biggie mine, as they say in Norway (literally translated to English).

I'd try to report that to Comeau.

EDIT: since Prasoon has also answered, quoting the Holy Standard with his interpretation of that contradicting what I wrote above, well, OK, standadeese…

§11.3/1 "The access of a member of a base class can be changed in the derived class…", and so on, which is as clear as can be (no interpretation required). And with a concrete example. And normative text stating that that is equivalent to a using declaration.

Cheers & hth.,

like image 112
Cheers and hth. - Alf Avatar answered Sep 18 '22 04:09

Cheers and hth. - Alf


The Standard in section 11.2/4 says

A member m is accessible when named in class N if

— m as a member of N is public, or

— m as a member of N is private, and the reference occurs in a member or friend of class N, or

— m as a member of N is protected, and the reference occurs in a member or friend of class N, or in a member or friend of a class P derived from N, where m as a member of P is private or protected, or

there exists a base class B of N that is accessible at the point of reference, and m is accessible when named in class B.

However the Standard also says that

§11.3/1 "The access of a member of a base class can be changed in the derived class.

In your code the access of the member foo has been changed in the derived class. So the code shouldn't compile but this is still an active issue with open status So some compilers compile the code (Comeau and Intel C++) whereas g++ and MSVC++ (correctly) reject it.

like image 38
Prasoon Saurav Avatar answered Sep 21 '22 04:09

Prasoon Saurav