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,048
  • 9
  • 44
  • 83
  • 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

4

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
  • 54,116
  • 13
  • 115
  • 176
  • 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
  • 216,937
  • 30
  • 383
  • 461
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
  • 196,531
  • 48
  • 417
  • 525
2
class S
{
    public:
        static S& getInstance()
        {
            static S    instance; 

            return instance;
        }
    private:
            // other stuff here
};
Saqlain
  • 16,750
  • 4
  • 26
  • 33
0

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,096
  • 6
  • 23
  • 38