Eric
Eric

Reputation: 2705

How to instantiate a singleton

I want to have one instance of the class C in my program, and I defined a singleton method get_instance as follows.

class C {
    static map<int, map<int, int> > t;
  public:
    static C& get_instance() {
        static C instance;
        return instance;
    }
  private:
    C() {};
};

and I tried to get a distance using this method.

static C& rt = C.get_instance();

However, I am getting an error

src/C.cpp:115:41: error: expected primary-expression before ‘.’ token
 static C& rt = C.get_instance();

Am I doing something wrong?

The Singleton design is from C++ Singleton design pattern

Upvotes: 1

Views: 1613

Answers (1)

timrau
timrau

Reputation: 23058

You should write C::get_instance().

Upvotes: 4

Related Questions