【发布时间】:2015-02-11 12:18:28
【问题描述】:
#include<iostream>
using namespace std;
class A{
int *numbers[5];
public:
void assignment(int ** x){
for(int i=0;i<5;i++)
numbers[i]=x[i]; //not changing just the value of *numbers[i] but the pointer numbers[i]
}
void print(){
for(int i=0; i<5;i++)
cout<< *numbers[i]<<endl;
}
};
int main(){
int *x[5];
for(int i; i<5;i++){
x[i]= new int(i);
cout<<*x[i]<<endl;
}
cout<<endl;
A numbers;
numbers.assignment(x);
numbers.print();
return 0;
}
我的问题非常具体。我想做与上面的代码相同的事情,但不是通过指针传递函数赋值(int **)的参数来通过引用来完成。我怎样才能做到这一点?
【问题讨论】:
-
我对你的问题和接受的答案有点困惑。如果使用
int *x[10];,则不能将其用作assignment()的参数。这是你希望完成的吗? -
嗯,这个问题只是为了教育目的,在我对通过引用传递的困惑中被问到。对于这个具体的例子,它回答了我的问题,因为整个问题是关于这个特定的代码段并且它有效。确实,您所指的内容需要与所选答案不同的内容。另外,你说的对我来说也是个问题,后来得到了答复。谢谢。
标签: c++ parameter-passing pass-by-reference pass-by-pointer