由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - static function and static variable?
相关主题
关于inline function里的static variablea C++ question
[合集] which design pattern is used if a static variable insidoperator overloading (C++)
static 变量放在哪里?C++access function static variable
请问static variable init的问题?Re: can destructor be static?
What're the three types of memory allocated for C++ variables?Global(static) variable initialization question
why use static function here?c 里面的local static variable
有没有static return type和static as function arguement?static variable存在heap还是stack?
How to check the virtual function table size?[合集] singleton and static
相关话题的讨论汇总
话题: static话题: obj话题: function话题: 子程序话题: variable
进入Programming版参与讨论
1 (共1页)
a***e
发帖数: 1140
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” 有什么含义?
c********e
发帖数: 383
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数组返回?
: }

N***m
发帖数: 4460
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

1 (共1页)
进入Programming版参与讨论
相关主题
[合集] singleton and staticWhat're the three types of memory allocated for C++ variables?
[合集] C问题求助:如何强行从外部访问local static variable?why use static function here?
[合集] static const代替define的performance tradeoff在哪里?有没有static return type和static as function arguement?
Static variables in functionHow to check the virtual function table size?
关于inline function里的static variablea C++ question
[合集] which design pattern is used if a static variable insidoperator overloading (C++)
static 变量放在哪里?C++access function static variable
请问static variable init的问题?Re: can destructor be static?
相关话题的讨论汇总
话题: static话题: obj话题: function话题: 子程序话题: variable