4

I know that there is a million questions and answers about Singletons out there but I just can't seem to find the solution to this. So running the risk of negative votes, here's my problem:

I want to use this singleton implementation from Andrei Alexandrescu' Modern C++ Design:

header:

class Singleton
{
    static Singleton& Instance();
  private:
    Singleton(){};
    Singleton(const Singleton&){};
    Singleton& operator=(const Singleton&){};
    ~Singleton(){};
};

implementation:

#include "s.hh"

Singleton& Singleton::Instance()
{
    static Singleton instance;
    return instance;
}

test:

#include "s.hh"

int main(void)
{
    Singleton& single = Singleton::Instance();
    return 0;
}

Now,

$g++ A.cc s.cc  && ./a.out 
In file included from A.cc:1:0:
s.hh: In function ‘int main()’:
s.hh:3:19: error: ‘static Singleton& Singleton::Instance()’ is private
 static Singleton& Instance();
               ^
A.cc:6:42: error: within this context
  Singleton& single = Singleton::Instance();
                                      ^

What is wrong with that? I am stuck...

steffen
  • 8,572
  • 11
  • 52
  • 90
  • Argh, that's what happens if you post a question before breakfast... In the original code I had the public specifier. I will post the "real" question in a minute. – steffen Apr 06 '13 at 08:03

5 Answers5

5

By default, a class' members are private. To access your singleton, you need to make Singleton::Instance public:

class Singleton
{
  // here!
  public:
    static Singleton& Instance();

  private:
    Singleton(){};
    Singleton(const Singleton&){};
    Singleton& operator=(const Singleton&){};
    ~Singleton(){};
};

Note that this is not the constructor (as you said in your title), it's the static member function that is supposed to return a reference to the singleton.

Daniel Frey
  • 55,810
  • 13
  • 122
  • 180
  • Where would be the implementation of Instance() function? Implementing in .cpp it complains about private ctor, while implementing in .h - works. – didinino Aug 13 '20 at 14:30
  • It should be possible in the .cpp. Did you forget the Singleton:: ? – Daniel Frey Aug 13 '20 at 18:34
3

Default access for a class is private, so you need to make the Instance() method explicitly public:

class Singleton
{
 public:
    static Singleton& Instance();
 private:
   // as before
....
};

Alternatively, you could use struct, whose default access specifiers are public:

struct Singleton
{
    static Singleton& Instance(); // public
  private:
    Singleton(){};
    Singleton(const Singleton&){};
    Singleton& operator=(const Singleton&){};
    ~Singleton(){};
};
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
3

The default access specifier for a class is private. Add the method under public access specifier.

public:
    static Singleton& Instance();

Good Read:
What are access specifiers? Should I inherit with private, protected or public?

Community
  • 1
  • 1
Alok Save
  • 202,538
  • 53
  • 430
  • 533
2
class S
{
    public:
        static S& getInstance()
        {
            static S    instance; 

            return instance;
        }
    private:
            // other stuff here
};
Saqlain
  • 17,490
  • 4
  • 27
  • 33
1

Also don't make your other destructor private.

class Singleton
{
  // here!
  public:
    static Singleton& Instance();
    ~Singleton(){};

  private:
    Singleton(){};
    Singleton(const Singleton&){};
    Singleton& operator=(const Singleton&){};

};
Pete B.
  • 3,188
  • 6
  • 25
  • 38