问题
Can anyone please tell me that why the functions parameters can not be static
?
Is this the reason that function parameters are declared on Stack
and gets de-allocated when function return? There is no way to retain parameter values? Just confused. Please clarify.
Thanks.
回答1:
The keyword static
can probably be seen as somewhat "overloaded".
The following usage-options are all viable:
- Static local variables
- Static global variables
- Static member variables
- Static global functions
- Static member functions
Variables:
In terms of runtime, all types of static variables are essentially the same. They all reside in the data-section of the program, and their addresses remain constant throughout the execution of the program. So the only difference between them is during compilation, in the scope of declaration:
- Static local variable: recognized by the compiler only in the scope of the function
- Static global variable: recognized by the compiler only in the scope of the file
- Static member variable: recognized by the compiler only in the scope of the class
Functions:
In terms of runtime, all types of functions (static and non-static) are essentially the same. They all reside in the code-section of the program, and their addresses remain constant throughout the execution of the program. So the only difference between them is during compilation, in the scope of declaration:
- Static global function: recognized by the compiler only in the scope of the file
- Static member function: recognized by the compiler only in the scope of the class
As to your question, arguments are passed to a function in the stack. There is no sense in having them static
, because that would effectively place them in the data-section. And if they are located in the data-section, then the function can simply read them from there instead of having them passed to it in the stack.
来源:https://stackoverflow.com/questions/22540877/why-function-parameters-can-not-be-static