473,544 Members | 1,842 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3362
> 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.merke rk(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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
3030
by: sks_cpp | last post by:
Are the standard library functions pertinent to both sequence containers and associative containers? For example, if "find_if", "remove_if", etc... valid for both lists, deques, vectors, sets, and maps? I know that the word "iterator" is typedef'd from something for everyone of these containers so I was curious if all these functions are...
14
2109
by: Michael Winter | last post by:
Is the Standard Template Library capable of storing complete objects rather than pointers. For example, would either of the vectors below (a, c) function correctly (note the member in C). class A {}; class B {}; class C { stl::vector<B> b; };
3
1415
by: velthuijsen | last post by:
just found the STL and now I'm mucking around with it to see how it works. First thing that I've found out is that I appearantly have no clue how copying works inside the STL (or more likely no clue how it works in general). So I've got two questions 1) Where can I find a good explanation on how this works on the internet? 2) Is the...
5
4223
by: Khalid | last post by:
II am allocating alot of memory for my problem model which uses stl containers for these pointers, will stl free the memory? by other words what is the semantics of memory ownership in stl? thanks a lot kh
3
1957
by: zero | last post by:
First a short discription of my program. I have a bot that connects to an internet chess server. Whenever the bot logs on, it loads certain information from file, and puts it in STL containers (specifically sets & maps). When disconnecting for any reason, the containers are converted to strings, and saved to disk, so they can be loaded...
43
4916
by: Steven T. Hatton | last post by:
Now that I have a better grasp of the scope and capabilities of the C++ Standard Library, I understand that products such as Qt actually provide much of the same functionality through their own libraries. I'm not sure if that's a good thing or not. AFAIK, most of Qt is compatable with the Standard Library. That is, QLT can interoperate with...
19
3376
by: AlesD | last post by:
Hello, I have problem that when I use std::list<MyClassand then store various subclasses of MyClass in that list (or any other STL container) the instances get sliced. I have read FAQ: ' What is a "virtual constructor"?', but I miss information if there is some "workaround" when using STL containers, since I can't change them to call...
35
2794
by: dragoncoder | last post by:
Just a simple theoritical question to the experts. What was the rationale behind making STL containers follow copy semantics rather than reference semantics. References almost always make things easier without much of overhead. Then why not reference ? Thanks /P
7
1459
by: mscava | last post by:
Hi... Today I was trying to find the best way to handle elements in Containers. I've found 3 appraoches but all do have pros and cons... 1. std::vector<MyClass> - inefficient - you have to write assing an copy constructor - to add new element into container you do need 2 lines of code + very comfortable use of algorhitms from STL 2....
0
7413
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7356
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7597
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7697
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5894
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5286
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4902
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3397
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
650
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.