LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-26-2010, 06:35 AM   #1
Accinson
LQ Newbie
 
Registered: Feb 2009
Posts: 22

Rep: Reputation: 6
C++: question about assignment of structures


Hi,
does the assignment operator copy all the members also for structures containing STL objects (strings,vectors,vectors of vectors...)?

I did try it on my platform,and it works that way (copying all the fields),but i was wondering if it is a standard behavior or not.

Thanks in advance!
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 09-26-2010, 06:40 AM   #2
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Yes it is standard behavior.

Assuming you do not define an assignment operator for your struct, the compiler will create an assignment operator for your struct and that assignment operator will use the assignment operators of each member of the struct.

The assignment operator for std::vector copies the contents (so a vector of vectors would be "deep copied" by assignment). Other STL object behave in similar manners.

But remember that assigning a pointer just copies the pointer. It does not copy the object. If your struct contains pointers, you should give some thought to how those pointers should be treated during assignment. Probably, you would want to write an assignment operator to implement the way you want the members that are pointers copied. Then you take over responsibility for assigning all the members, not just those that are pointers.

std::vector internally uses a pointer for its buffer space. So inside the source code of std::vector it was not possible to use the default assignment operator that the compiler would provide. Instead the source code of std::vector includes an assignment operator that correctly assigns that pointer. When you use std::vector you don't need to worry about those details. When you use pointers in structs (as std:vector does internally) you do need to worry about those details.

Last edited by johnsfine; 09-26-2010 at 06:52 AM.
 
2 members found this post helpful.
Old 09-26-2010, 06:53 AM   #3
Accinson
LQ Newbie
 
Registered: Feb 2009
Posts: 22

Original Poster
Rep: Reputation: 6
Thank you :-)
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Question on variable naming and assignment... ${manintheyellowhat} Linux - Newbie 6 07-26-2010 07:41 PM
Gateway assignment and DHCP+DHRELAY Question Lord_Devi Linux - Networking 1 07-12-2010 04:29 PM
Structures AMMullan Programming 6 02-18-2004 11:39 AM
File Assignment Question JC404 Linux - Newbie 4 07-03-2003 08:15 PM
array of structures scanf entry question ... purpleburple Programming 21 10-01-2002 11:08 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:31 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration