Created
January 15, 2023 19:24
-
-
Save Infinitusvoid/ad38bcef30a0588329b05412a7446a8f to your computer and use it in GitHub Desktop.
C++ : What does static mean outside the class or struct ?
This file contains hidden or 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> | |
int variable_v0 = 500; | |
void func_a() | |
{ | |
std::cout << "I am func_a" << std::endl; | |
} | |
static void func_b() | |
{ | |
std::cout << "I am func_b" << std::endl; | |
} |
This file contains hidden or 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> | |
// use static as much as you can if you don't need the variable to be global | |
// otherwise linker is going to be able to pick it up everywhere. | |
// extern means it will look for the variable or function in the external translation unit | |
// which is called external linkage or external linking | |
extern void func_a(); | |
extern void func_b(); | |
//static int variable_v0 = 20; | |
extern int variable_v0; //500 | |
//int variable_0; // error already defined in other translation unit | |
int main() | |
{ | |
func_a(); // ok linker has found it external translation unit | |
//func_b(); // liner error ( in file_0 it's marked as static so it's limeted to that translation unit) | |
std::cout << variable_v0; | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment