/* | |
* Example of a singleton design pattern. | |
* Copyright (C) 2011 Radek Pazdera | |
* This program is free software: you can redistribute it and/or modify | |
* it under the terms of the GNU General Public License as published by | |
* the Free Software Foundation, either version 3 of the License, or | |
* (at your option) any later version. | |
* This program is distributed in the hope that it will be useful, | |
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
* GNU General Public License for more details. | |
* You should have received a copy of the GNU General Public License | |
* along with this program. If not, see <http://www.gnu.org/licenses/>. | |
*/ | |
#include <iostream> | |
class Singleton | |
{ | |
private: | |
/* Here will be the instance stored. */ | |
static Singleton* instance; | |
/* Private constructor to prevent instancing. */ | |
Singleton(); | |
public: | |
/* Static access method. */ | |
static Singleton* getInstance(); | |
}; | |
/* Null, because instance will be initialized on demand. */ | |
Singleton* Singleton::instance = 0; | |
Singleton* Singleton::getInstance() | |
{ | |
if (instance == 0) | |
{ | |
instance = new Singleton(); | |
} | |
return instance; | |
} | |
Singleton::Singleton() | |
{} | |
int main() | |
{ | |
//new Singleton(); // Won't work | |
Singleton* s = Singleton::getInstance(); // Ok | |
Singleton* r = Singleton::getInstance(); | |
/* The addresses will be the same. */ | |
std::cout << s << std::endl; | |
std::cout << r << std::endl; | |
} |
This comment has been minimized.
This comment has been minimized.
Good Example to show singleton. Thank you. |
This comment has been minimized.
This comment has been minimized.
thank you,very good |
This comment has been minimized.
This comment has been minimized.
how to implement in header only library. |
This comment has been minimized.
This comment has been minimized.
Is this implementation thread-safe? |
This comment has been minimized.
This comment has been minimized.
Your implementation has a memory leak. |
This comment has been minimized.
This comment has been minimized.
nice example! |
This comment has been minimized.
This comment has been minimized.
Very helpful! Thanks a lot! |
This comment has been minimized.
This comment has been minimized.
Nice, however, I just would say that there is a memory leak. When you create a raw pointer you need to make sure to delete it in the destructor. If not then make sure to new a smart pointer so it gets deleted automatically. |
This comment has been minimized.
This comment has been minimized.
The copy constructor and the copy assignment operator should be declared private, i.e. private: Otherwise, you will be able to clone your object. If you are using C++ 11, you may leave the copy constructor and the copy assignment operator public but explicitly delete them: public: |
This comment has been minimized.
This comment has been minimized.
Nice, but I have some notes here:
|
This comment has been minimized.
This comment has been minimized.
how can i access same object every time for my program }; int main()
} |
This comment has been minimized.
This comment has been minimized.
The use of "static" inside the function getInstance() makes things even cleaner:
|
This comment has been minimized.
This comment has been minimized.
Singletons like this in multi-threaded environments. Worth a read. |
This comment has been minimized.
This comment has been minimized.
If you delete constructor with |
This comment has been minimized.
This comment has been minimized.
In what manner? Since their initialisation singletons are supposed to live as long as the application does, so you don't have to worry about that raw pointer as once the application execution ends, the application memory will be freed by the system anyway. |
This comment has been minimized.
This comment has been minimized.
This is very bad example, first copy assignment and copy ctors should be deleted, also you fail to release memory. |
This comment has been minimized.
This comment has been minimized.
This example is from 9 years ago. why you all people doing same critism again again like a parrot.When author write to this example , probably C++ 11 was not available. |
This comment has been minimized.
This comment has been minimized.
Is this implementation thread-safe? |
This comment has been minimized.
This comment has been minimized.
for C++03 you should declare copy constructor and operator and not implement them to work around |
This comment has been minimized.
This comment has been minimized.
I see a bunch of people complain that memory is leaked. No it is not. The operating system will recover the memory (it essentially acts as a global variable). |
This comment has been minimized.
This comment has been minimized.
no i will make it |
This comment has been minimized.
This comment has been minimized.
no i will make it |
This comment has been minimized.
This comment has been minimized.
This will make a new object. To prevent that you must delete copy constructor or make it private
|
This comment has been minimized.
This comment has been minimized.
//I implement this way
|
This comment has been minimized.
This comment has been minimized.
@SwarajKetan Your singleton gets never created. You could either add a new to your getInstance implementation or avoid pointers completely like here:
|
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
https://stackoverflow.com/questions/55490024/stdcall-once-when-should-it-be-used |
This comment has been minimized.
If this is a header .h file, class implements should be written in .cpp file
Otherwise, compile error occurs.