-
-
Save ddemidov/4527904 to your computer and use it in GitHub Desktop.
static global vs static member
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
#ifndef A_H | |
#define A_H | |
template <class T> | |
struct A { | |
static int a; | |
}; | |
template <class T> | |
int A<T>::a; | |
static int sa; | |
#endif |
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 <iostream> | |
#include "a.h" | |
int show_a_1(); | |
int show_a_2(); | |
int show_sa_1(); | |
int show_sa_2(); | |
int main() { | |
A<int>::a = 42; | |
sa = 42; | |
show_a_1(); | |
show_a_2(); | |
show_sa_1(); | |
show_sa_2(); | |
} |
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
$ g++ -o hello hello.cpp s1.cpp s2.cpp | |
$ ./hello | |
A::a(1): 42 | |
A::a(2): 42 | |
sa(1): 0 | |
sa(2): 0 |
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 <iostream> | |
#include "a.h" | |
int show_a_1() { | |
std::cout << "A::a(1): " << A<int>::a << std::endl; | |
} | |
int show_sa_1() { | |
std::cout << "sa(1): " << sa << std::endl; | |
} |
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 <iostream> | |
#include "a.h" | |
int show_a_2() { | |
std::cout << "A::a(2): " << A<int>::a << std::endl; | |
} | |
int show_sa_2() { | |
std::cout << "sa(2): " << sa << std::endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment