Last active
September 11, 2018 20:10
-
-
Save jeffotoni/495385d54e4a1f2f3c6a121b20ae9613 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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 de nossa instancia */ | |
DriverPg(); | |
public: | |
int port; | |
/* Metodo static public */ | |
static DriverPg* Connect(); | |
}; | |
/* Null, porque a instancia ira | |
inciar 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