Nested classes accessing member of parent class

Started by
6 comments, last by Red Ant 15 years, 9 months ago
Is there any easy way of making this work? I want the method do_something(), which is defined in a nested class, to have access to members of the parent class.

#include <iostream>

using namespace std;

class some_class
{
public:
	int some_number;

	class
	{
	public:
		int do_something()
		{
			some_number++;
		}
	} some_object;

	some_class()
	{
		some_numer = 0;
	}
};

int main()
{
	some_class blah;

	blah.some_object.do_something();
	
	cout << blah.some_number << endl;

	system("pause");
	return 0;
}

Advertisement
Nested classes have no special access to their parent classes.

You have to declare the parent class a friend in the nested class in order to access private and protected members.

I should add that your example code makes no sense.
Yea, I'll have to do something like that.

I was just hoping C++ supported some special thing that would allow me to do it.

Thanks anyway.
If you want that you'll have to pass your nested class'es constructor a reference pointing to an instance of the parent class ... OR ... make the members you want to access static ... then you don't need an instance.


class OuterClass{private:    int m_privateInt;    // static member ... don't forget to initialize this in the cpp    static int m_privateStaticInt;public:    class InnerClass    {    public:        explicit InnerClass( OuterClass& ref ) : m_outer( ref ) {}        void do_something()        {            // access static member ... no instance needed to do this            OuterClass::m_privateStaticInt = 20;            // access non-static member via reference to OuterClass            m_ref.m_privateInt = 5;        }    private:        OuterClass& m_ref;    };};
Quote:Original post by fpsgamer
You have to declare the parent class a friend in the nested class in order to access private and protected members.


Where do you see him trying to access private / protected members of the nested class? His problem aren't the access specifiers of the members, his problem is that his nested class needs a reference to an object of the outer class.

Anyway, note that nested (inner) classes always have full access to all members of the parent (outer) class, regardless of whether they're public, protected or private.
Quote:Original post by tendifo
I was just hoping C++ supported some special thing that would allow me to do it.

C++ is not Java.
Quote:Original post by Red Ant
Quote:Original post by fpsgamer
You have to declare the parent class a friend in the nested class in order to access private and protected members.


Where do you see him trying to access private / protected members of the nested class? His problem aren't the access specifiers of the members, his problem is that his nested class needs a reference to an object of the outer class.


Thats exactly the reason why I said that his code snipped doesn't make any sense. The problem is that even if he did have access to an instance of the parent class, he would would only have access to public members as evidenced by the quote that follows.

Quote:Original post by Red Ant
Anyway, note that nested (inner) classes always have full access to all members of the parent (outer) class, regardless of whether they're public, protected or private.



ISO/IEC 14882:2003 11.8.1
The members of a nested class have no special access to members of an
enclosing class, nor to classes or functions that have granted friend-
ship to an enclosing class; the usual access rules (_class.access_)
shall be obeyed. The members of an enclosing class have no special
access to members of a nested class; the usual access rules
(_class.access_) shall be obeyed.


It is worth noting that compilers like MSVC and gcc handle this situation differently because they implement a proposed fix to a different bug. However IBM XL C++, for example, doesn't.

But the standard is very clear. So the choice you make in the end depends on your compiler and what kind of portability you expect.
Darn, well in that case please accept my apologies. I could have sworn I'd read something in "The C++ Programming Language" that said nested classes had full access to the enclosing class, but I suppose I may have read that on msdn instead then.

This topic is closed to new replies.

Advertisement