相关疑难解决方法(0)

在C++中通过引用传递指针的原因?

在哪种情况下你想在c ++中使用这种性质的代码?

void foo(type *&in) {...}

void fii() {
  type *choochoo;
  ...
  foo(choochoo);
}
Run Code Online (Sandbox Code Playgroud)

c++ pointers reference

89
推荐指数
4
解决办法
9万
查看次数

使用指针传递引用和值

我不明白为什么传递指向函数的指针不会改变传入的数据.如果函数proto看起来像这样:

void func( int *p ); 
Run Code Online (Sandbox Code Playgroud)

和func分配内存到p,为什么不能在函数外部使用?我以为指针是地址?

c++ parameter-passing pass-by-reference pass-by-value

13
推荐指数
2
解决办法
2万
查看次数

对作为函数参数传递的指针使用delete

删除已作为函数参数传递的指针(如下所示)是否合法(和合法):

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

这个编译好了,但我只是想确保它永远都是那样.

c++ memory-management new-operator

9
推荐指数
3
解决办法
1万
查看次数