Thread: Reference to singleton as member

  1. #1
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972

    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:
    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?
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  2. #2
    Distributed Programming beyonddc's Avatar
    Join Date
    Dec 2001
    Location
    Mass
    Posts
    31
    You should use a pointer instead of a reference, becaue it is a singleton class, you don't want to have ownership to the singleton instance.

    By the way, the sample you've is just a quick write-up, right?
    I see there's some syntax that is incorrect.

    Also, keep in mind that if you're doing multi-threaded programming, you need to make sure there will be no two threads accessing the getInstance() get the same time.

    Most people apply a mutex in the getInstance() function to synchronize the function.
    Last edited by beyonddc; 01-27-2006 at 10:12 PM.
    -dc

  3. #3
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    >>You should use a pointer instead of a reference, becaue it is a singleton class, you don't want to have ownership to the singleton instance.

    Hmm I don't quite see what difference it makes...In any case, I'll use a pointer--it doesn't really seem to make much of a difference as far as I can see

    >>By the way, the sample you've is just a quick write-up, right?
    I see there's some syntax that is incorrect.

    Yes but I don't see the incorrect syntax

    And I'm not doing anything multithreaded
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Try;
    Code:
       Foo(): x(*Single::instance()){}
    Initialise a reference with a reference, not with a pointer. Either that, or change Single::instance() so it returns a reference, not a pointer.

  5. #5
    Distributed Programming beyonddc's Avatar
    Join Date
    Dec 2001
    Location
    Mass
    Posts
    31
    Quote Originally Posted by JaWiB
    >>You should use a pointer instead of a reference, becaue it is a singleton class, you don't want to have ownership to the singleton instance.

    Hmm I don't quite see what difference it makes...In any case, I'll use a pointer--it doesn't really seem to make much of a difference as far as I can see
    If you own a reference to the singleton instance, basically it means you've the ownership of that piece of memory. You can set the reference to point to null. If some other classes come in later and would like to obtain an instance of the singleton, then they'll get a bad reference.


    Quote Originally Posted by JaWiB
    >>By the way, the sample you've is just a quick write-up, right?
    I see there's some syntax that is incorrect.

    Yes but I don't see the incorrect syntax

    And I'm not doing anything multithreaded
    Code:
    if(inst==0) inst = new Single; // your compilation will fail here.
    
    if(inst==0) inst = new Single(); // aahh... much better :)
    -dc

  6. #6
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Quote Originally Posted by grumpy
    Initialise a reference with a reference, not with a pointer. Either that, or change Single::instance() so it returns a reference, not a pointer.
    Ah...yeah that might work better heh
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> If you own a reference to the singleton instance, basically it means you've the ownership of that piece of memory. You can set the reference to point to null. If some other classes come in later and would like to obtain an instance of the singleton, then they'll get a bad reference.

    This is not true. You cannot set a reference to null. You cannot set the object referred to by the reference to null, as it will not affect any other references to that object. At worst, you can delete the Single object, but you can do that with a pointer as well, and presumably the destructor is private or protected which prohibits that anyway.

    >> if(inst==0) inst = new Single; // your compilation will fail here.

    That syntax is valid.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Can you check what is wrong with this code
    By Ron in forum C++ Programming
    Replies: 4
    Last Post: 08-01-2008, 10:59 PM
  3. Replies: 6
    Last Post: 07-29-2008, 04:37 AM
  4. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  5. Problem with OpenGL tutorial
    By 2Biaz in forum Windows Programming
    Replies: 18
    Last Post: 09-16-2004, 11:02 AM