Standard containers, delete, copy

G

Gandalf

Hello. I have some questions about the standard containers.

How does the standard containers behave if I do

queue<Foo> myQ;
queue<Foo> myQ2;
.... insert into myQ...

myQ = myQ2;

Will it call the copy constructor for each element in the queue and insert
them into the other queue?

or,

queue<Foo* > myQ3;
queue<Foo* > myQ4;

myQ3 = myQ4;

What will happen here?

(Assuming I have a properly implemented class Foo with CC, operator=,
destructor and so on)

If I destroy a queue object, will the destructor be called for each element
in the queue?
 
G

Gandalf

If I destroy a queue object, will the destructor be called for each
element in the queue?
What I meant here was,
If I have a class

class Foo{
int* p;
queue<Bar> myQ;
}

What would a destructor for that class look like?

Foo::~Foo(){
delete p; //assuming p is pointing to something
What should I do with myQ?
}
 
K

Karl Heinz Buchegger

Gandalf said:
Hello. I have some questions about the standard containers.

How does the standard containers behave if I do

queue<Foo> myQ;
queue<Foo> myQ2;
... insert into myQ...

myQ = myQ2;

Will it call the copy constructor for each element in the queue and insert
them into the other queue?

Each container holds its own objects. So assigning one container to another
has to create copies of the objects. I'm not sure if this is done through
the copy ctor or with the help of a default ctor and an assignment op, but
I would think it is the fist one. If in doubt make a test program and
output some text in both functions, then you will see.
or,

queue<Foo* > myQ3;
queue<Foo* > myQ4;

myQ3 = myQ4;

What will happen here?

Both continer hold identical pointers.
Those pointers point to the same object.
(Assuming I have a properly implemented class Foo with CC, operator=,
destructor and so on)

If I destroy a queue object, will the destructor be called for each element
in the queue?

The STL container manage what you store in them. So if you store objects
in them, the conainer manages the objects. If you store pointers in them
the containers manage the pointers and it is your reponsibility to
manage the objects those pointers point to.
 
B

Bob Smith

Gandalf said:
What I meant here was,
If I have a class

class Foo{
int* p;
queue<Bar> myQ;
}

What would a destructor for that class look like?

Foo::~Foo(){
delete p; //assuming p is pointing to something
What should I do with myQ?
}

nothing, AFAIK the Bar is desctructed when the myQ goes out of scope,
I.E. Foo is desctructed.

on the other hand if you have queue<Bar*> myQ then you need to delete
each object of type Bar* in the queue
TTBOMK
/B
 
K

Karl Heinz Buchegger

Gandalf said:
What I meant here was,
If I have a class

class Foo{
int* p;
queue<Bar> myQ;
}

What would a destructor for that class look like?

Foo::~Foo(){
delete p; //assuming p is pointing to something
What should I do with myQ?

// nothing at all.
// the queue object takes care of what is stored in it
// (same with all other STL containers)
 
P

Peter van Merkerk

What I meant here was,
If I have a class

class Foo{
int* p;
queue<Bar> myQ;
}

What would a destructor for that class look like?

Foo::~Foo(){
delete p; file://assuming p is pointing to something
What should I do with myQ?
}

Nothing, myQ will be destroyed after Foo's destructor has finished. All
Bar objects in myQ will be destroyed by the std::queue<Bar> destructor.

Note that if you would store pointers instead of objects, and those
pointers should be deleted, then you have to delete them yourself in the
Foo destructor. A std::queue<Bar*> destructor only destroys the pointers
themselves, not the objects they are pointing to.
 
G

Gandalf

// nothing at all.
// the queue object takes care of what is stored in it
// (same with all other STL containers)
Thanks.
Sometimes I get so uncertain about this, it's a long learning process.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top