Jump to content
  • Advertisement
Sign in to follow this  
jorgander

C++ template friend class and inheritance

This topic is 2856 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 don't see why what I'm trying to do shouldn't be possible, but for some reason I can't get VC++ 2008 Express to like it. Here's the basic idea:


namespace Script
{
template <typename T, typename GlobalTraits> class Global;

class Registry
{
private:
class GlobalInternal
{
...
};

template <typename T, typename GlobalTraits> friend Script::Global;
};

template <typename T, typename GlobalTraits>
class Global : private Registry::GlobalInternal
{
...
};
}



With the above code I get this error:

1>d:\development\cpp\assware\common\include\asw\script.h(291) : error C2955: 'ASW::Script::Global' : use of class template requires template argument list
1> d:\development\cpp\assware\common\include\asw\script.h(234) : see declaration of 'ASW::Script::Global'


Does anyone know the correct syntax?

In case anyone asks, Registry::GlobalInternal is supposed to be inaccessible from any class that isn't supposed to have access to it, but still have it separate from Global so that source code that uses it can be compiled on its own (i.e. not use a template class). If anyone knows of a cleaner way to accomplish that I would like to know of it.

Share this post


Link to post
Share on other sites
Advertisement
	template <typename T, typename GlobalTraits> friend class Script::Global;

Is that what you were looking for?

Share this post


Link to post
Share on other sites
Quote:
Original post by alvaro
	template <typename T, typename GlobalTraits> friend class Script::Global;

Is that what you were looking for?


I originally thought so, but upon adding class I am presented with this compiler error:


1>d:\development\cpp\assware\common\include\asw\script.h(315) : error C2248: 'ASW::Script::Registry::GlobalInternal' : cannot access private class declared in class 'ASW::Script::Registry'
1> d:\development\cpp\assware\common\include\asw\script.h(271) : see declaration of 'ASW::Script::Registry::GlobalInternal'
1> d:\development\cpp\assware\common\include\asw\script.h(237) : see declaration of 'ASW::Script::Registry'
...


The error leads me to believe that the compiler is not matching the friend declaration with prior forward declaration, although I'm not sure of this.

Share this post


Link to post
Share on other sites
Did you try:

template <typename T, typename GlobalTraits> friend class Global;

Using Script::Global is probably making the compiler think you're trying to friend ::Script::Script::Global rather than ::Script::Global.

Share this post


Link to post
Share on other sites
I did try that, and I got the same error as in my second post. I also removed the forward declaration and tried it both ways - same errors.

Interestingly enough, changing the relationship from "is-a" to "has-a" compiles without error:


template <typename T, typename GlobalTraits>
class Global
{
private:
Registry::GlobalInternal internal;
...
};



Anyone know why? I would think the same access rules apply.

Perhaps it has something to do with when the compiler is loading the class - with "has-a" relationship the compiler has already read the class declaration and checked access rules, but with "is-a" it is still reading the class declaration (i.e. the class name and bases) when it encounters the base class name.

Share this post


Link to post
Share on other sites
Removing the Script:: seems to work fine with inheritance with MSVC 2010. Might be time to upgrade.

Share this post


Link to post
Share on other sites
Upgrading to VC++ Express 2010 does not fix it, so I'm assuming they fixed it only in the paid version. Unfortunately I'll have to go with composition for now.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!