I want to create a vector of object A (aArray) and manipulate the value of aArray.a when I call the Heap constructor. Currently, the values of aArray.a remain unchanged. How can I do this properly?
Thank you for your help!
#include <vector>
#include <iostream>
struct A
{
A(int av):a(av){}
int a;
};
struct Heap
{
Heap(std::vector<A> *hVal)
{
for (int i=0; i<hVal->size(); ++i)
{
hVec.push_back(hVal->at(i));
}
std::cout << std::endl;
for(int i=0; i<hVec.size(); ++i)
{
hVec[i].a*=2;
std::cout << "hVec[i].a*=2 is " << hVec[i].a << "\n";
}
}
std::vector<A> hVec;
};
int main()
{
A a0(2), a1(4), a2(8);
std::vector<A> aArray;
aArray.push_back(a0);
aArray.push_back(a1);
aArray.push_back(a2);
Heap h(&aArray);
for(int i=0; i<aArray.size(); ++i)
{
std::cout << "aArray[i].a = " << aArray[i].a << "\n";
}
return 0;
}
Your
Heapconstructor copies*hValtohVec, then modifieshVec. If you want it to updateaArrayin main, modify*hValinstead (for example viahVal->at(i).a *= 2;).Update: