Created
August 15, 2014 02:52
-
-
Save imetallica/b5ea464c9263c3e7c85e 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
#include <vector> | |
#include <iostream> | |
class Example { | |
public: | |
static int STATIC_VARIABLE; | |
static std::vector<Example> examples; | |
int NON_STATIC_VARIABLE; | |
Example(int mod) { | |
this->NON_STATIC_VARIABLE = mod; | |
} | |
~Example(){} | |
}; | |
int Example::STATIC_VARIABLE = 666; | |
std::vector<Example> Example::examples; | |
int main() { | |
std::vector<Example>::iterator it; | |
it = Example::examples.begin(); | |
for(int i = 0; i < 10; i++) { | |
it = Example::examples.insert(it, *(new Example(i))); | |
} | |
it = Example::examples.begin(); | |
for(int i=0; i< 10; i++) { | |
// Variaveis static devem ser chamadas utilizando o operador "::" | |
// Elas sao unicas, mesmo que voce chame dezenas de vezes o operador new | |
// para criar uma nova referencia a classe. | |
// Variaveis nao-static devem ser chamadas utilizando o operador "." | |
// Cada nova chamada de new vai criar uma nova instancia do atributo. | |
std::cout << "Non static variable: " << Example::examples[i].NON_STATIC_VARIABLE << "\n"; | |
std::cout << "Static variable: " << Example::STATIC_VARIABLE << "\n"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment