0
#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 **)的参数来通过引用来完成。我怎样才能做到这一点?

4

1 回答 1

4

利用:

void assignment(int* (&x)[5]) ...

编辑:对于评论“如果长度......不是标准......”,您可以使用模板:

template<int N> void assignment(int* (&x)[N]) ...

编译器会自动推导出 N。

于 2014-12-12T18:12:33.827 回答