Archived

This topic is now archived and is closed to further replies.

nested classes and friend

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

I want to let my enclosing class access private members of a nested class using friend. This code works
class a
{
private:
	class b
	{
		friend class a;
	private:
		int o;
	};

	b g;	

public:
	void v()
	{
		g.o = 5;
	}
};

int main()
{
	a g;
	g.v();
	return 0;
}
but when I do it with a template class I get a compiler error cannot access private member declared in class ''a::b''
template <class T> class a
{
private:
	class b
	{
		friend class a;
	private:
		int o;
	};

	b g;	

public:
	void v()
	{
		g.o = 5;
	}
};

int main()
{
	a <int> g;
	g.v();
	return 0;
}
I think it''s a bug in vc6, because gcc doesn''t have any trouble with it. Am I doing something wrong, or is it a really a bug? My Site

Share this post


Link to post
Share on other sites
I got thinking about this again last night and I'm not sure about one thing.

When the template is instantiated for more than one datatype, won't there be duplicate definitions of class b?

Since they differ only by the line:

friend class a<T>;

could this cause a problem?

Maybe it would be better to also template class b in terms of T as well?

Can anyone with some experience shed light on this?

[edited by - SpaceRogue on July 9, 2003 4:54:55 PM]

Share this post


Link to post
Share on other sites
No, there won't be multiple definitions because the full name of b is a<T>::b, so for each T there will be a different name for B.

EDIT: doh, damn templates and HTML...

[edited by - Neosmyle on July 10, 2003 5:18:26 PM]

Share this post


Link to post
Share on other sites