#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;
}
My question is very specific. I want to do the same thing as the code above but instead of passing the argument of function assignment(int **) by pointer to do it by reference. How can I achieve that?
Use:
void assignment(int* (&x)[5]) ...
edit: for the comment "if the length... wasn't standard...", you can use a template:
template<int N> void assignment(int* (&x)[N]) ...
The compiler will automatically deduce N.