- Question about structure object copy
- Posted by Kid on May 29th, 2008
Hi
I find that some sample write some code like this
typedef struct _X
{
int a;
int b;
} X;
struct X A,B;
A.a = 1;
A.b = 2;
......
B=A;
I found this code works and I can change struct to class and test object
copy in
C++ sample.
I used to think we should use memcpy for structure and class object copy .
Can I use operator "=" handle object copy ?
Thank for your teaching.
- Posted by Maxim S. Shatskih on May 29th, 2008
Yes.
Default compiler-generated "operator =" is defined as calling "operator =" on
all fields and base class instances.
For a simple non-class type like int or pointer, "operator =" is a bitwise
copy.
So, in C++, if there is a C struct without "operator =" defined, then "=" will
mean bitwise copy field by field, which is the same meaning as in C.
--
Maxim Shatskih, Windows DDK MVP
StorageCraft Corporation
maxim@storagecraft.com
http://www.storagecraft.com
- Posted by Doron Holan [MSFT] on May 29th, 2008
are you sure that it actually calls overloaded operator='s on all fields in
C++. IIRC, the default operator= just does a bitwise copy for the size of
the structure
d
--
Please do not send e-mail directly to this alias. this alias is for
newsgroup purposes only.
This posting is provided "AS IS" with no warranties, and confers no rights.
"Maxim S. Shatskih" <maxim@storagecraft.com> wrote in message
news:OMrcCvTwIHA.5832@TK2MSFTNGP02.phx.gbl...
- Posted by 440gtx@email.com on May 30th, 2008
Structure copy has always been legal and should be preferred over
memcpy since it is more clear, concise, and less error prone. You
sound very new to programming. Tip: simplify your code if using .cpp
as below by removing the unnecessary baggage. The less stuff you have
to read the quicker and easier the code is to understand.
struct X
{
int a;
int b;
};
X A;
A.a = 1;
A.b = 2;
X B(A); // an even better way to copy a struct 
There is no requirement to change the word "struct" to "class" in C++.
"struct" can do everything "class" can--inheritance, private data,
functions inside, etc. When I learned the only subtle difference
between the two, I decided never to use "class" again.
Yes. And in C++ you can add intelligence to "=" by defining this
operator if appropriate. With C++ you even have the flexibility to
DENY structure copy if you don't want it such that A=B fails! Or you
can define structure equality "if (A==B)". The sky is the limit. Being
restricted to C means one must use this hack: if
(IsEqualGUID(a,b)) ... but if you use C++ you can do this: if (a ==
b) ... much better.
- Posted by Maxim S. Shatskih on May 30th, 2008
This is according to good old book of Stroustrup/Ellis of early 90ies.
Compiler-generated "operator =" is generated as a sequence of invocations of
"operator =" first for all base class instances in the declaration order of the
base classes, then to all fields in declaration order of the fields.
In this context, "invocation" is a call if the particular "operator =" function
really exists, i.e. it is present in the code or is compiler-generated. If the
function does not exist (i.e. simple type like int or pointer) - it is a
bitwise copy.
The book is old, but I do not think such a basic notions could change since
that times.
So, "operator =" turns to memcpy only if there are no manually written
"operator =" for any base classes or fields, or there are no base classes at
all.
In the other words: let's introduce the notion of "simple-copied" type. The
type is simple-copied if either:
a) it is a non-class type like int or pointer
b) all its fields and all its base classes are simple-copied
operator= is memcpy only for simple-copied classes.
--
Maxim Shatskih, Windows DDK MVP
StorageCraft Corporation
maxim@storagecraft.com
http://www.storagecraft.com
- Posted by Ray Trent on May 30th, 2008
440gtx@email.com wrote:
The main reason to use "class" vs. "struct" is a social and commenting
one. "class" communicates to other programmers that the object is
probably not a simple data structure, but has significant semantics.
Whereas "struct" communicates to other programmers that the object is
(almost entirely) just a data buffer.
People that override operator"=" (well... ever, but especially) for
structs deserve to be shot. Or to maintain their code for the rest of
their sorry lives, which will make them *wish* they'd been shot.
--
Ray