Reference to singleton as member
Shouldn't it be possible to keep a reference to a singleton class as a member? For example:
Code:
class Single
{
public:
static Single* instance(){
if(inst==0) inst = new Single;
return inst;
}
private:
static Single* inst;
};
Single* Single::inst = 0;
class Foo
{
public:
Foo():x(Single::instance()){}
private:
Single& x;
};
The errors I get:
Quote:
main.cpp(20): error C2439: 'Foo::x' : member could not be initialized
main.cpp(22) : see declaration of 'Foo::x'
main.cpp(20): error C2354: 'Foo::x' : initialization of reference member requires a temporary variable
The compiler is telling me I can't initialize a reference to a temporary variable, but it isn't really temporary is it? Maybe I should just use a pointer for the member variable, or is there a much better way to do this?