1

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

Community
  • 1
  • 1
Eric
  • 2,635
  • 6
  • 26
  • 66

1 Answers1

4

You should write C::get_instance().

timrau
  • 22,578
  • 4
  • 51
  • 64
  • Thank you. Is it because it's a static method? – Eric Nov 17 '14 at 05:43
  • 1
    @user2418202 exactly. – timrau Nov 17 '14 at 05:43
  • 2
    @user2418202: for `static` members you're effectively using the class name as a scoping mechanism (akin to a namespace), and the same `::` notation should be used; whereas for non-static members you're actually doing something with a specific runtime object instance of the class (namely `*this`), and `.` is used for that.... – Tony Delroy Nov 17 '14 at 06:03