I don't know why this is driving me nuts but it is. I have a function defined and forward declared in main.
static void myFunc(int x);
static void myFunc( int x)
{
//do stuff
}
main()
I want to use myFunc(int x) in another class. So I would think all I have to do is extern static void myFunc(int x) within that classes header and then just call it where I need to in the class definition, but it won't work.
What am I doing wrong?
Thanks
Static variables in C have the following two properties: They cannot be accessed from any other file. Thus, prefixes “ extern ” and “ static ” cannot be used in the same declaration. They maintain their value throughout the execution of the program independently of the scope in which they are defined.
You cannot use extern and static together they are mutually exclusive. You need to use only extern if you need External Linkage.
static means a variable will be globally known only in this file. extern means a global variable defined in another file will also be known in this file, and is also used for accessing functions defined in other files. A local variable defined in a function can also be declared as static .
The extern keyword in C and C++ extends the visibility of variables and functions across multiple source files. In the case of functions, the extern keyword is used implicitly. But with variables, you have to use the keyword explicitly.
You cannot use extern and static together they are mutually exclusive.
static
means Internal Linkageextern
means External Linkage
You need to use only extern
if you need External Linkage.
Good Read:
what is external linkage and internal linkage in c++?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With