> Why do you think that listPointer is unnecessary as heap allocation?
you know at compile time the type of the object that you want to create.
you don't pretend to use polymorphism (there are no virtual functions in std::list)
it doesn't "survive" its scope.
So it would be equivalent to
std::list<CA> listPointer;
Bjarne Stroustrup wrote: |
---|
Code that creates an object using new and then deletes it at the end of the same scope is ugly, error-prone, and inefficient. |
¿what is your reason to use dynamic allocation?
2. As I said, you are missing a proper copy/move constructor.
In your function `putList()' you create `aObject', then you try to push_front() it, which would either move or copy it (same thing in your case)
When the function ends, aObject is destroyed. Its destructor is invoked and
delete[] heapPointer
executed.
the problem is that the copy that were in the list has the same value for `heapPointer'. It was pointing to the same location, so now it becomes invalid.
To solve this you need to provide an adequate copy/move constructor.
An easy way
std::vector<int> heapPointer;