Jump to content
  • Advertisement

Archived

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

I thought STL containers destroyed there contents

This topic is 5104 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
Advertisement
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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!