Jump to content
  • Advertisement
Sign in to follow this  
dmatter

why can a template member access a private nested class?

This topic is 4825 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 stumbled on this by accident and I really don't understand why it works:
struct base
{

private: //IT IS PRIVATE!!!
    struct yo
    {
        static void execute() { /*do something*/}
    };
};

struct derived : public base
{
    template <class T>
    T Burn(T i) { yo::execute(); return i; }
};
Why can the templated 'Burn' access base::yo::execute()? I think perhaps its some non-standards error with my compiler - For this I'm using the Dev-Cpp 4.9.9.2 IDE with some Mingw port ofa GNU compiler. Should this work? Does this work on other compilers? Why does it work? To have a guess I'd say its something to do with the fact the Burn is a template and execute is a static member but wont compile if Burn is made a non-template. Any suggestions? Thanks

Share this post


Link to post
Share on other sites
Advertisement
you could probably do better by posting it somewhere todo with gcc/g++

they might thank you for obscure bug find( if that is what it is )

Share this post


Link to post
Share on other sites
it tried it - it compiles and runs

what else could it be?

my only thought is maybe the windows port of gcc/g++ is behind the main "linux" version.

maybe its already been fixed. maybe not.

my suggestion:

http://gcc.gnu.org/bugs.html

Share this post


Link to post
Share on other sites
As was said, you have to actually instantiate the template. There's nothing wrong until an instantiation of the function is actually created. This is not a problem with your compiler. This is a good thing.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
The code shoud fail to compile as struct yo is declared private. If you want derived to be able to access it, it needs to be declared as either protected or public. VS.net 2003 gives the error message:

***.cpp(22) : error C2248: 'base::yo' : cannot access private struct declared in class 'base'

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!