By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,728 Members | 2,462 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,728 IT Pros & Developers. It's quick & easy.

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

P: n/a
--------- 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?

Jul 23 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
lovecreatesbeauty wrote:

--------- 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.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.