Skip to content

Instantly share code, notes, and snippets.

@marcopaganini
Forked from jeffotoni/singleton-class.cpp
Last active September 11, 2018 20:12
Show Gist options
  • Save marcopaganini/3987b42e31f2b999849214a0a8e2fff7 to your computer and use it in GitHub Desktop.
Save marcopaganini/3987b42e31f2b999849214a0a8e2fff7 to your computer and use it in GitHub Desktop.
/*
* Example DriverPg C++
* @package main
* @author @jeffotoni
* @size 10/09/2018
*/
#include <iostream>
// ex: c++
class DriverPg {
private:
// este objeto sera nossa instancia estatica
static DriverPg* instance;
// Construtor da nossa instancia
DriverPg();
public:
int port;
// Metodo static public
static DriverPg* Connect();
};
// Null, porque a instancia ira iniciar conforme for chamada.
DriverPg* DriverPg::instance = 0;
// nosso método
DriverPg* DriverPg::Connect() {
if (instance == 0) {
instance = new DriverPg();
instance->port = 5432;
}
return instance;
}
DriverPg::DriverPg()
{}
int main() {
//new DriverPg(); // nao irá funcionar
DriverPg* s = DriverPg::Connect(); // Ok
DriverPg* r = DriverPg::Connect();
int p = DriverPg::Connect()->port;
// objetos retornados
std::cout << s << std::endl;
std::cout << r << std::endl;
std::cout << p << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment