Destructor called on objects in deque without it being called explicitly
I feel there's a bit of a misunderstanding going on between C++ and me...
My problem is that I have a deque of pointers to a class, and create objects then add them to the deque in a for loop. However, the destructor is called on the object as soon as the loop quits. Code:
#include <deque> Code:
i is now 0 Code:
i is now 1 So, I suppose my question is: what is the correct way to create that deque of pointers to the abstract class? I don't seem to be barking up the right tree currently. Thanks in advance :) |
You've declared a local object in your for-loop, which gets destroyed with each iteration of the loop.
Code:
Test temp; Code:
Test* temp = new Test; |
Brilliant, thank you very much.
With regards to your postscript, do you just mean that when popping the objects I should do: Code:
delete mydeque.back(); |
Quote:
Code:
Test* test = mydeque.back(); |
Yes, sorry, I just meant for how I go about deleting the object. Thankyou :)
However, it turns out there's an additional complication due to the fact that I'm using an abstract class. Code:
#include <deque> EDIT: Never mind. For others asking the same question, read this article on virtual destructors. I fixed it by adding a "virtual ~MetaTest(void)" declaration and defining it as "MetaTest::~MetaTest(void) { }" Thanks again, |
All times are GMT -5. The time now is 05:50 AM. |