[Query] Copying an object of a class to another one
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
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
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?
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 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.
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.
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).
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,
— 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.
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