Skip to content

Instantly share code, notes, and snippets.

@imetallica
Created August 15, 2014 02:52
Show Gist options
  • Save imetallica/b5ea464c9263c3e7c85e to your computer and use it in GitHub Desktop.
Save imetallica/b5ea464c9263c3e7c85e to your computer and use it in GitHub Desktop.
#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