Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does GCC allow inheriting from a private nested class?

Consider the following code:

class A {
  class B {};
};

template <typename C>
class D : A::B {};

void f() {
  D<int> d;
}

D<int> inherits from A::B which is a private nested class. I was expecting this to be an error, but GCC accepts this code. Is it a bug in GCC or am I missing something?

like image 366
vitaut Avatar asked Dec 11 '12 23:12

vitaut


People also ask

Do nested classes inherit?

A nested class has access to all the private members of its enclosing class—both fields and methods. Therefore, a public or protected nested class inherited by a subclass has indirect access to all of the private members of the superclass.

How do I access private nested classes?

Accessing the Private Members Write an inner class in it, return the private members from a method within the inner class, say, getValue(), and finally from another class (from which you want to access the private members) call the getValue() method of the inner class.

Can nested classes access private members?

A nested class is a member of its enclosing class. Non-static nested classes (inner classes) have access to other members of the enclosing class, even if they are declared private.

What is a nested class what are its advantages how it is defined and declared in C ++?

A nested class is a class which is declared in another enclosing class. A nested class is a member and as such has the same access rights as any other member. The members of an enclosing class have no special access to members of a nested class; the usual access rules shall be obeyed.


2 Answers

I've found the answer. Since it's might be useful for others I am posting it here - this is GCC bug 47346.

like image 193
vitaut Avatar answered Sep 20 '22 16:09

vitaut


Did you try to create non template derivering class?

Template class is not compiled if there is no object of that class. Try to create instance of this class or create non-template derived class - gcc will probably fail ;-)

Edit My bad - the object is created and it's not causing gcc error. Sorry for that.

like image 32
Yester Avatar answered Sep 23 '22 16:09

Yester