或者太太,在C++中,我正在考虑使用子程序来定义我在主体中首先声明的所有指针.我知道可以通过使用函数每次返回一个指针来完成.因此,我仍然希望在子程序中执行此操作.我google了很多,还没有找到答案.非常感谢您的帮助.示例c ++代码如下:
#include <iostream>
using namespace std;
void testsub(int* k3d)
{
k3d= new int [10];
cout<<"test 0 "<<k3d[0]<<endl;
}
int main ()
{
int* i3d=0;
testsub(i3d);
cout<<"test 1 "<<i3d[0]<<endl;
}
Run Code Online (Sandbox Code Playgroud)
我希望在子程序中定义了虚拟指针k3d后,可以使用主体中的i3d.非常感谢提前.
在花了一些时间挖掘相关帖子/在线资源后,我仍然对我的问题感到困惑.我的示例代码(test.cc)是:
void testsub(const int* &xx );
int main ()
{
int* xx;
xx= new int [10];
testsub(xx);
}
void testsub(const int* & xx){}
Run Code Online (Sandbox Code Playgroud)
编译错误消息(pgcpp)读取
"test.cc", line 7: error: a reference of type "const int *&" (not const-qualified)
cannot be initialized with a value of type "int *"
testsub(xx);
^
1 error detected in the compilation of "test.cc"."
为什么?非常感谢您的帮助.祝福,婷