Sign in to follow this  

Nested classes accessing member of parent class

This topic is 3460 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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;
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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;
};
};



Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 3460 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this