using const keyword

I am having a question related to const keyword. I want to know which is the correct way to use it. I have seen both these implementations while I was trying to read about singleton patterns - const Singleton & and Singleton const &. Here is one such example
1
2
3
4
5
6
7
8
9
10
11
class Singleton{
public: 
   static Singleton& getInstance(){
      static Singleton inst;
      return inst;
   }
private:
   Singleton(){};
   Singleton(const Singleton&);
   Singleton& operator=(const Singleton&);
}


Which is the correct one to use "const Singleton&" or "Singleton const&"
Last edited on
Thanks @Little Bobby Tables. I have a follow on question. Does pointer precedence rule in stack overflow link apply to references also. In other words, const Singleton& and Singleton const& are not the same
that is beyond me. i do not know. i always go with const foo &the var
Does pointer precedence rule in stack overflow link apply to references also. In other words, const Singleton& and Singleton const& are not the same


No, references are not pointers.

I'm wondering if you understand what you read, though. Were that a pointer, the declarations you supply here would be the same.

See: http://ideone.com/LTRRa0
Remember, read it right to left:
const Singleton& is a reference to a Singleton (const).
Singleton const& is a reference to a const Singleton.
Singleton& const is a constant reference to a Singleton.

Note the last one is considered illegal - references are constant anyway, you can't change what they refer to, so there is no point to having a constant reference.
Thanks @cire and @NT3. That helps.
Topic archived. No new replies allowed.