-
-
Save marcopaganini/3987b42e31f2b999849214a0a8e2fff7 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 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