Can Nested class access parent class's private member?

This is a discussion on Can Nested class access parent class's private member? within the C++ Programming forums, part of the General Programming Boards category; I write the following code. It seems that a nested class (inner class) can access its parent class's private member. ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    284

    Can Nested class access parent class's private member?

    I write the following code. It seems that a nested class (inner class) can access its parent class's private member. BUT in the book <thinking in C++> 's chapter :introduction to template, it mentions that we must declare the nested class as friend. And says this is the style how STL's iterator is implemented.

    Is this true?

    Code:
    #include <iostream>
    
    class MyVector{
    private:
    	int x;
    public:
    	MyVector():x(0){}
            //class iterator; 
    	//friend class iterator; // do we really need to make it a friend?
    
    	class iterator{
    	public:
    		iterator(){	}
    		void foo(MyVector& a){
    			a.x=4;
    			return;
    		}
    	};
    	void print(){
    		std::cout<<x<<std::endl;
    	}
    };
    
    int main()
    {
    	MyVector obj;
    	MyVector::iterator nested;
    	nested.foo(obj);
    	obj.print();
    	return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,980
    Interesting. This is a modified version of an example given in section 11.8 of the 2003 edition of the C++ Standard:
    Code:
    class E {
        int x;
        class B { };
        class I {
            B b; // error: E::B is private
            int y;
            void f(E* p, int i)
            {
                p->x = i; // error: E::x is private
            }
        };
        int g(I* p)
        {
            //return p->y; // error: I::y is private
            return 0;
        }
    };
    
    int main(){}
    The comments are given as in the text of the Standard, except that I commented out the line that the third comment comments on and added a return 0;

    The above code compiles without warnings on the MinGW port of g++ 3.4.5 and the Comeau online compiler (both with C++0x extensions enabled and disabled). MSVC8 (from Visual Studio 2005) only gives a warning about an unused variable.

    Consequently, it seems that according to the text of the current standard, all these compilers do not conform with respect to access control where nested classes are concerned. So, your book is right and these compilers are wrong (unless it is a defect in the Standard itself).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    C++03 11.8p1 says: "The members of a nested class have no special access to members of an enclosing class."

    However, the resolution to Defect Report 45 undoes this special exception:
    "A nested class is a member and as such has the same access rights as any other member."

    However, despite this change being proposed in 2001, it somehow missed the deadline for C++03 and didn't make it in; it was subsequently part of the first candidate draft of C++0x and will be in there. However, since its status is "Defect", compiler writers are encouraged to make this semantic change in their C++03 implementations, too.

    As such, the compilers are perfectly correct in allowing the access.


    (Blocking the access of nested classes was a stupid idea anyway. Java did the right thing there.)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Registered User
    Join Date
    Jun 2009
    Posts
    3

    Vs6.0

    It seems this is not allowed by Visual Studio 6.0.

    I've just come across the same issue because code I can compile using g++ (mingw v3.4.2) won't compile under Visual Studio 6.0. Making the inner class a friend of the outer, as in your example, makes it work.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,980
    Quote Originally Posted by major_tom3
    It seems this is not allowed by Visual Studio 6.0.
    That would be expected since the implementation of MSVC6 is based on draft versions of the 1998 edition of the C++ standard, but the defect report that CornedBee cited is from 2001.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 02-14-2008, 01:59 PM
  2. Replies: 2
    Last Post: 04-02-2006, 04:59 PM
  3. webBrowser problem
    By algi in forum C# Programming
    Replies: 7
    Last Post: 08-19-2005, 09:35 AM
  4. why does this have private member access?
    By major_small in forum C++ Programming
    Replies: 9
    Last Post: 07-16-2004, 01:52 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21