Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ singleton GetInstance() return

When implementing a singleton in C++, is it better for GetInstance() to return a pointer to the singleton object, or a reference? Does it really matter?

like image 498
Marcin Avatar asked Jan 15 '09 19:01

Marcin


1 Answers

I prefer a reference. I use reference instead of a pointer whenever I want to document that:

  • It can't be null
  • It won't be changed (to point to something else)
  • It mustn't be deleted
like image 150
ChrisW Avatar answered Sep 21 '22 21:09

ChrisW