Sign in to follow this  
Nathaniel Hammen

private nested class friend functions

Recommended Posts

With this code:
class Class
{
	class NestedClass
	{
		private:
			Class* This;
			Class* That;
			void Func();

		public:
			friend void FriendFunc(NestedClass* &);
	};

//Other Stuff
};

void FriendFunc(Class::NestedClass* &NestedObjectPtr)
{
	NestedObjectPtr->Func();
}
It says "cannot access private class declared in class 'Class'" The only way that I can define FriendFunc is like this:
class Class
{
	class NestedClass
	{
		private:
			Class* This;
			Class* That;
			void Func();

		public:
			friend void FriendFunc(NestedClass* &NestedObjectPtr);
			{
				NestedObjectPtr->Func();
			}
	};

//Other Stuff
};
With large functions, this looks ugly. Is there a way to do this?

Share this post


Link to post
Share on other sites
mike25025    494
class Class
{
class NestedClass
{
private:
Class* This;
Class* That;
void Func();

public:
friend void FriendFunc(NestedClass* &);
};

//Other Stuff
public:
friend void FriendFunc(NestedClass* &);

};

Share this post


Link to post
Share on other sites
Kaijin    163
FriendFunc can't access Class::NestedClass because it has private visibility. You need to declare NestedClass as public or declare FriendFunc as a friend of Class also.

Share this post


Link to post
Share on other sites
FlamELorD    122
Well i guess this is kinda out of topic.. but isnt just easier to declare the function in other place and then use it in the class???

or... what's the deal about using Nested classes? whats the good thing about it??

Share this post


Link to post
Share on other sites

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