jeudi 30 août 2018

Can not return pointer array in the struct

I am new to C++. below is my code. I saw a correct array in "setValue", but in "main", I can not get the right array value on strC. where did I miss?

template <int N>
struct strA{
int *p;
};

template <int N>
strA<N> setValue(int n)
{
strA<N> strB;
int m[N];
// pointer initialization
strB.p=m;
for (int i=0; i<N;i++)
{
    strB.p[i]=i+n;
}
return strB;
}

int main(){
const int N=3;
strA<N> strC;
strC=setValue<N> (5);
for (int i=0; i<N;i++)
{
    cout<< strC.p[i]<<endl;
}
return 0;
}

Aucun commentaire:

Enregistrer un commentaire