Board index » cppbuilder » copy of object

copy of object


2005-02-24 05:50:12 AM
cppbuilder114
Is there any way that I can make a copy of a object without assigning all the member variables one by one?
Thanks.
 
 

Re:copy of object

"didan" < XXXX@XXXXX.COM >writes:
Quote
Is there any way that I can make a copy of a object without
assigning all the member variables one by one? Thanks.
In some special cases, yes, but the general answer is "no".
If your object contains only types that are declarable in the C
language, does not have any inheritance, constructors, or member
functions or pointers, then you should be able to safely memcpy
the data from the source into the target.
For C++ objects, member-wise copying and assignment is how data is
moved around.
What is your actual concern? You just don't like typing in all those
assignments, or is there another issue at play?
--
Chris (TeamB);
 

Re:copy of object

My object includes member functions and pointers. I guess I can't make the copy simple. Thanks Chris
Chris Uzdavinis (TeamB) < XXXX@XXXXX.COM >wrote:
Quote
"didan" < XXXX@XXXXX.COM >writes:

>Is there any way that I can make a copy of a object without
>assigning all the member variables one by one? Thanks.

In some special cases, yes, but the general answer is "no".

If your object contains only types that are declarable in the C
language, does not have any inheritance, constructors, or member
functions or pointers, then you should be able to safely memcpy
the data from the source into the target.

For C++ objects, member-wise copying and assignment is how data is
moved around.

What is your actual concern? You just don't like typing in all those
assignments, or is there another issue at play?

--
Chris (TeamB);
 

{smallsort}

Re:copy of object

"didan" < XXXX@XXXXX.COM >writes:
Quote
My object includes member functions and pointers. I guess I can't
make the copy simple. Thanks Chris
The member function isn't necessarily a problem unless any of them are
virtual. (Because then there is the hidden vtbl pointer to worry
about.)
As for copying the object containing pointers, the problem is the
typical "pointer aliasing" bug, where two objects both point to the
same object, and both objects think they "own" it. At some point, an
object is destroyed, and the other can be left with a dangling pointer
to a deleted object.
If your pointers do not have ownership semantics of the thing to which
they point, then you can also safely copy them. (Assuming you're not
using any kind of reference-counting or other scheme that requires
knowing about all pointers to a particular address.)
--
Chris (TeamB);