Skip to content

Instantly share code, notes, and snippets.

@mathieu-b
Created August 10, 2017 10:09
Show Gist options
  • Save mathieu-b/0eb8f029650e09375c369fc1b90ac448 to your computer and use it in GitHub Desktop.
Save mathieu-b/0eb8f029650e09375c369fc1b90ac448 to your computer and use it in GitHub Desktop.
Singleton lifecycle.
/******************************************************************************
Online C++ Compiler.
Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.
https://www.onlinegdb.com/online_c++_compiler
*******************************************************************************/
#include <iostream>
#include <string>
void printy(std::string msg)
{
std::cout << "MSG: " << msg << std::endl;
}
class Gino {
public:
static Gino & getInstance()
{
printy("Gino::getInstance()");
static Gino instance;
return instance;
}
void sayHello()
{
printy("Gino says: Hello!");
}
private:
Gino()
{
printy("Gino::Gino()");
}
~Gino()
{
printy("Gino::~Gino()");
}
};
int main()
{
printy("=== C++ Singleton life cycle ===");
printy("Entered main() ...");
Gino::getInstance().sayHello();
printy("... Exiting main() .");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment