Returning static reference

Why is the following code not working as expected?. Im having some code in the anaonomouy block of a C++ implmentation [cxx] file.

1
2
3
4
5
6
7
8
namespace
{
bool & hasAccess()
{
static bool haveAccess = false;
return haveAccess;
}
}


and in one of my method implementations, I do:

1
2
3
4
5
6
void myMethod()
{

hasAccess() = true; // But this is not setting the static boolean haveAccess to true.

}



Please let me know what I am missing.

Thanks,
Pavan.
Last edited on
Works fine for me: http://coliru.stacked-crooked.com/a/129cf78636d489d9

Are you sure that you have the hasAccess function in the same file as the myMethod function you have there?

Also, why are you doing that? It's so much more readable and simpler to do the same thing with just a static bool in the required file or a bool in your anonymous namespace.
Last edited on
Topic archived. No new replies allowed.