LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 05-27-2008, 12:20 AM   #1
jaepi
Member
 
Registered: Apr 2007
Location: Urban Jungle
Distribution: Ubuntu
Posts: 189
Blog Entries: 1

Rep: Reputation: 30
[Query] Copying an object of a class to another one


Hello there, I have a question about creating a copy of an object. I know that copying an object is done this way:
Code:
Object* A;
Object* B;
:
:
//populate Object A here
:
:

B = A;
What if you do it this way:

Code:
Object* A;
Object* B;
:
:
//populate Object A here
:
:
memcpy(B,A,sizeof(Object));
Object here is a class.

I tried using both and I haven't experienced any weird thing.

What is the difference between the two? Is the second one wrong? What is the advantage and disadvantage?
 
Old 05-27-2008, 03:23 AM   #2
clvic
Member
 
Registered: Feb 2008
Location: Rome, Italy
Distribution: OpenSuSE 11.x, vectorlinux, slax, Sabayon
Posts: 206
Blog Entries: 2

Rep: Reputation: 45
If the two objects are not actually the same class (but they are of the same base class indeed), for sure you'll have problems because not everything would be copied - say the derived class has some more attributes than the base one, these would be skypped.
And also, copying with memcpy will skip doing anything else than copying, while the Object constructor can do many other things - opening files, manipulating global variables, and so on...
Copying via the assignment operator will respect all these things, and is in my opinion the right way
 
Old 05-27-2008, 04:14 AM   #3
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Quote:
memcpy(B,A,sizeof(Object));
If Object is a POD type then copying via memcpy is guaranteed to create a copy which has the same state, else it is unknown what will happen and this is one of the reasons that the programmer should provide a copy constructor, assignment operator and a constructor. The default assignment operator which can be generated by the language uses a member wise copy, which maybe a byte for byte copy just the same as using memcpy, this can be referred to as a shallow copy. What you may require is a deep copy using your own defined assignment operator, for example if Object contains an int pointer and a byte for byte copy is made the two class instances both contain the same pointer. Which instance owns the pointer and therefore which will delete it, if any?

Quote:
B = A;
No copy of the object is created here only a pointer is copied so there is still only one instance of the object.

For reference
Quote:
For any POD type T, if two pointers to T point to distinct T objects obj1 and obj2, where neither obj1
nor obj2 is a base-class subobject, if the value of obj1 is copied into obj2, using the memcpy library
function, obj2 shall subsequently hold the same value as obj1.
Quote:
Arithmetic types (3.9.1), enumeration types, pointer types, and pointer to member types (3.9.2), and cvqualified
versions of these types (3.9.3) are collectively called scalar types. Scalar types, POD-struct types,
POD-union types (clause 9), arrays of such types and cv-qualified versions of these types (3.9.3) are collectively
called POD types.
Quote:
12.8 Copying class objects [class.copy]
1 A class object can be copied in two ways, by initialization (12.1, 8.5), including for function argument
passing (5.2.2) and for function value return (6.6.3), and by assignment (5.17). Conceptually, these two
operations are implemented by a copy constructor (12.1) and copy assignment operator (13.5.3).
Quote:
11 A copy assignment operator for class X is trivial if it is implicitly declared and if
— class X has no virtual functions (10.3) and no virtual base classes (10.1), and
— each direct base class of X has a trivial copy assignment operator, and
— for all the nonstatic data members of X that are of class type (or array thereof), each such class type has
a trivial copy assignment operator;
otherwise the copy assignment operator is non-trivial.
12 An implicitly-declared copy assignment operator is implicitly defined when an object of its class type is
assigned a value of its class type or a value of a class type derived from its class type. A program is illformed
if the class for which a copy assignment operator is implicitly defined has:
— a nonstatic data member of const type, or
— a nonstatic data member of reference type, or
— a nonstatic data member of class type (or array thereof) with an inaccessible copy assignment operator,
or
— a base class with an inaccessible copy assignment operator.
Before the implicitly-declared copy assignment operator for a class is implicitly defined, all implicitly declared
copy assignment operators for its direct base classes and its nonstatic data members shall have
been implicitly defined.
Quote:
The implicitly-defined copy constructor for class X performs a memberwise copy of its subobjects. The
order of copying is the same as the order of initialization of bases and members in a user-defined constructor
(see 12.6.2). Each subobject is copied in the manner appropriate to its type:
— if the subobject is of class type, the copy constructor for the class is used;
— if the subobject is an array, each element is copied, in the manner appropriate to the element type;
— if the subobject is of scalar type, the built-in assignment operator is used.
Virtual base class subobjects shall be copied only once by the implicitly-defined copy constructor (see
12.6.2).
http://www.informit.com/guides/conte...lus&seqNum=309

Last edited by dmail; 05-27-2008 at 04:48 AM.
 
Old 05-28-2008, 01:51 AM   #4
jaepi
Member
 
Registered: Apr 2007
Location: Urban Jungle
Distribution: Ubuntu
Posts: 189

Original Poster
Blog Entries: 1

Rep: Reputation: 30
Thanks guys.
 
  


Reply

Tags
c++, class, clone, copy



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
tough (to me) c++ questions about pointer to class object reference and more parv Programming 6 01-11-2008 09:26 PM
Pass object variable to anothor class in JAVA hus Programming 2 05-11-2005 04:04 AM
Talk to an object in a diferent class bastl Programming 9 03-17-2005 01:36 PM
C++ class-object? shivaligupta Programming 2 01-06-2005 02:25 AM
Event driven object-to-object: C++ template class mecanism ( NOT STL or STDC++) bretzeltux Programming 2 12-23-2003 02:45 PM

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

All times are GMT -5. The time now is 05:20 PM.

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