为什么函数参数不能是静态的 [英] Why function parameters can not be static

查看:139
本文介绍了为什么函数参数不能是静态的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以告诉我为什么函数参数不能为static吗? 这是在Stack上声明函数参数并在函数返回时取消分配的原因吗?有没有办法保留参数值?只是感到困惑.请澄清.

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.

谢谢.

推荐答案

关键字static可能被视为有些超载".

The keyword static can probably be seen as somewhat "overloaded".

以下用法选项都是可行的:

The following usage-options are all viable:

  • 静态局部变量
  • 静态全局变量
  • 静态成员变量
  • 静态全局函数
  • 静态成员函数

就运行时而言,所有类型的静态变量本质上都是相同的.它们都位于程序的数据部分中,并且它们的地址在整个程序执行期间保持不变.因此它们之间的唯一区别是在编译期间,在声明的范围内:

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:

  • 静态局部变量:仅在函数范围内由编译器识别
  • 静态全局变量:仅在文件范围内由编译器识别
  • 静态成员变量:仅在类范围内,编译器才能识别

就运行时而言,所有类型的功能(静态和非静态)基本相同.它们都驻留在程序的代码段中,并且它们的地址在整个程序执行期间保持不变.因此它们之间的唯一区别是在编译期间,在声明的范围内:

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是没有意义的,因为那样会将它们有效地放置在数据部分中.而且,如果它们位于data-section中,则该函数可以简单地从那里读取它们,而不必将它们传递到堆栈中.

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.

这篇关于为什么函数参数不能是静态的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆