vendredi 26 décembre 2014

Pointer to std::vector has different data in different scopes

In my class I want to pass a container to constructN that will store all constructed elements.


Basically if I call constructN with a pointer to std::vector that vector will only change it's data inside of constructN and behaves like a vector that just got constructed. Any state that vector had before is restored as soon as that function returns. The pointer point to the same address though. I can't seem to find a solution for this..



template<class T> class MemManager {

template<class container, class ...Args> void constructN(size_t n, container* c, Args&&...args) {
for (size_t i = 0; i < n; n++) {
c->push_back(allocator.allocate(1));
c->size(); // == 1, 2, 3, 4, ...
c; // == 0x24fe00
/** constructing... **/
}
}
}

int main() {
MemManager<int>* mm = new MemMamager<int>;
std::vector<int*>* objects = new std::vector<int*>; // == 0x24fe00
objects->push_back(new int);

mm.constructN(10, objects);
objects->size(); // == 1;
objects; // == 0x24fe00

delete mm;

return 0;
}

Aucun commentaire:

Enregistrer un commentaire