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
|
|