Sign in to follow this  

private nested class friend functions

This topic is 4823 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

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

This topic is 4823 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