在哪种情况下你想在c ++中使用这种性质的代码?
void foo(type *&in) {...}
void fii() {
type *choochoo;
...
foo(choochoo);
}
Run Code Online (Sandbox Code Playgroud) 我不明白为什么传递指向函数的指针不会改变传入的数据.如果函数proto看起来像这样:
void func( int *p );
Run Code Online (Sandbox Code Playgroud)
和func分配内存到p,为什么不能在函数外部使用?我以为指针是地址?
删除已作为函数参数传递的指针(如下所示)是否合法(和合法):
#include<iostream>
class test_class{
public:
test_class():h(9){}
int h;
~test_class(){std::cout<<"deleted";}
};
void delete_test(test_class* pointer_2){
delete pointer_2;
}
int main(){
test_class* pointer_1;
while(true){
pointer_1 = new test_class;
//std::cout<<pointer_1->h;
delete_test(pointer_1);
}
}
Run Code Online (Sandbox Code Playgroud)
这个编译好了,但我只是想确保它永远都是那样.