471,570 Members | 995 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,570 software developers and data experts.

Standard containers, delete, copy

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?

Jul 19 '05 #1
6 3235
> 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?
}
Jul 19 '05 #2


Gandalf wrote:

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.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #3


Gandalf wrote:
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?


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


--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #4
> // 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.
Jul 19 '05 #5
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; 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.

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl
Jul 19 '05 #6


Gandalf wrote:
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?
}


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

Jul 19 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by sks_cpp | last post: by
14 posts views Thread by Michael Winter | last post: by
5 posts views Thread by Khalid | last post: by
3 posts views Thread by zero | last post: by
43 posts views Thread by Steven T. Hatton | last post: by
19 posts views Thread by AlesD | last post: by
35 posts views Thread by dragoncoder | last post: by
7 posts views Thread by mscava | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by lumer26 | last post: by
reply views Thread by Vinnie | last post: by
reply views Thread by lumer26 | last post: by
reply views Thread by lumer26 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.