Archived

This topic is now archived and is closed to further replies.

I thought STL containers destroyed there contents

This topic is 4942 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a vector like so ->
class A()
{
public:
A() { cout << "constructer called." << endl;
~A() {  cout << "destructer caleld." << endl;
};

int main()
{
vector<A *> vlpA(1, new A);
return 1;
}
The constructer is called, but not the destructor. One of the whole points of using std containers like vector, deque, and string were that they automatically destroyed the objects when they leave scope. What gives?

Share this post


Link to post
Share on other sites
The container is holding pointers to objects not objects. So it''s destroying pointers.

This is probally what you want

#include <iostream>
#include <vector>
using namespace std;

class A{
public:
A() { cout << "constructer called." << endl;}
~A() { cout << "destructer caleld." << endl;}
};

int main(){
vector<A> vlpA(1, A());
return 1;
}

Share this post


Link to post
Share on other sites
Because I''m working with base classes that point to derived classes, I''ll have to use koens 2nd method. I have to use pointers, I can''t use non pointed to objects. Arrrgh, so I GOTTA manually delete the contents of vector. This is a pain. Hmm, maybe I shoudl use make a vector of autopointers to A*. But this could be a lot of bloated code! Thanx for all help.

Share this post


Link to post
Share on other sites
quote:
Original post by Eriond
Why return 1, doesn''t that signal an error, or something?


Your right it should return 0, I copied and pasted.

I don''t think the operating system checks return codes. But 0 is supposed to mean sucessful run. Return codes really matter in stuff like shell scripts (very important for linux shell scripts).

Share this post


Link to post
Share on other sites
A plethora of informationt to digest. Thanx for all the informative replies. I''m really just looking for a solution where I can have an expandable type of array that holds pointers to objects. I didn''t want to have to write a Linked List. But since where I need this code will be called a 20-50 times in every main loop iteration, I believe a vector of boost::shared_ptr will be too slow. I think i''ll have to end up using a Singlely linked list :[

Share this post


Link to post
Share on other sites

template <class Seq>
void tidy(Seq& container)
{
typename container::iterator it;
for (it = container.begin(); it != container.end(); ++it)
{
delete *it;
*it = 0;
}
}
tidy(vlpA); // boom



[edited by - fallenang3l on May 30, 2004 9:06:54 PM]

Share this post


Link to post
Share on other sites
not to evangelize the act of reinventing the wheel, but writing a linked list is a good exercise

... and besides, once you write it you can use it in any application you make in the future! and you''ll know exactly what it does

Share this post


Link to post
Share on other sites