When copy object of class type, all members shouldn't be non-public?

L

lovecreatesbeauty

--------- quotation ---------
The default method of initialization using special member functions is
to perform a bit-for-bit copy from the initializer into the object to
be initialized. This technique is applicable only to:
1. Objects of built-in types.
2. Pointers.
3. References.
4. Objects of class type, where the class has no private or protected
members, no virtual functions, and no base classes.
--------- of of quotation ---------



I saw these description when read some documents.

Does the fourth item says that when perform initialization using copy
ctor or assignment using operator =, all members of the class can't be
non-public. For example do this:

//Class C1;
C1 o;

C1 o2 = o;
o2 = o;


But even though deriving from a base class, there isn't such a
limitation. Why the limitation can be applied for copying object from a
same class type? Is it true in iso/iec 14882?
 
K

Karl Heinz Buchegger

lovecreatesbeauty said:
--------- quotation ---------
The default method of initialization using special member functions is
to perform a bit-for-bit copy from the initializer into the object to
be initialized. This technique is applicable only to:
1. Objects of built-in types.
2. Pointers.
3. References.
4. Objects of class type, where the class has no private or protected
members, no virtual functions, and no base classes.
--------- of of quotation ---------

When a point 5 is added (And I guess the author ment it to be that way)
5. Objects of class type, where each data member is a POD
then the whole thing is the descirption of what a POD is.

POD = Plain Old Data structre

or in other words: A struct as it was in C. A collection of members
without anything special.
I saw these description when read some documents.

Does the fourth item says that when perform initialization using copy
ctor or assignment using operator =, all members of the class can't be
non-public.

No. It just says that only in that case the compiler will do a bit-for-bit
initialization (or assignment). What the author didn't say: In all other cases
a memberwise copy or assignment is done.

In practice this isn't something you have to worry about. The compiler will
initialize a class the right way. Only in some cases he can use a somewhat
faster method.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top