Redian新闻
>
static function and static variable?
avatar
static function and static variable?# Programming - 葵花宝典
a*e
1
用了C++一段时间了,还是没有完全弄明白。
看一个程序:
主程序:
double* obj; //只有定义。
function_sub(&obj); //调用子程序, 并把指针的地址做参量。
子程序:
static void function_sub(double **obj_p){
static double val[2]={1,2}; //为什么要static?
*obj_p=val; //让local数组返回?
}
俺不明白的是,参数 obj 只定义,没有分配内存。 当子程序调用结束时,local数组就
自动distory了,那返回的难道不是一个无效数组首地址吗? 还是因为加了static 关键
字就变成global了?
还有在这种情况子函数定义成 “static void” 有什么含义?
avatar
c*e
2

this static keyword doesnt really mean much in this context. only thing it
says is that the definition is within the file scope, unless this is a member
function of a class/struct.
this static is the key for what happens, I am not saying that this code is
applausible but yes, the obj points to the val now. since it is static, it is
used as some global var now.
BTW, i dont think that this is C++ code , looks like some amature C programmer
's idea. If it is really a c++ coder, he would go with t

【在 a***e 的大作中提到】
: 用了C++一段时间了,还是没有完全弄明白。
: 看一个程序:
: 主程序:
: double* obj; //只有定义。
: function_sub(&obj); //调用子程序, 并把指针的地址做参量。
: 子程序:
: static void function_sub(double **obj_p){
: static double val[2]={1,2}; //为什么要static?
: *obj_p=val; //让local数组返回?
: }

avatar
N*m
3
c++ 这么做好吗?避免引用新的关键字,但是老是让人犯糊涂。

member
is
is
programmer

【在 c********e 的大作中提到】
:
: this static keyword doesnt really mean much in this context. only thing it
: says is that the definition is within the file scope, unless this is a member
: function of a class/struct.
: this static is the key for what happens, I am not saying that this code is
: applausible but yes, the obj points to the val now. since it is static, it is
: used as some global var now.
: BTW, i dont think that this is C++ code , looks like some amature C programmer
: 's idea. If it is really a c++ coder, he would go with t

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。