Reprinted please indicate the source, Contact information: hongszh at gmail.com
After reading "Deep Exploration ",In C ++ object model, I came across the comments of memberwise and bitwise. I think memberwise is a general name initialized by members in C ++, bitwise is a special case in the initialization process. Here I will summarize memberwise and bitwise based on my understanding. If this is not the case, you are welcome to give good comments to friends interested in this topic.
Memberwise Initialization
InSection P49, default memberwise initialization: If a class does not provide an explicit copy construct, when this class object uses another object of the same class as the initial value, internally, memberwise Initialization is used. That is, copy the value of each built-in or derived data member (such as a pointer or an array) from one object to another, however, it does not copy the member Class Object (this case should be because the Member class does not contain the copy constructor, whether explicitly stated by the class designer or synthesized by the compiler ), instead, memberwise Initialization is implemented recursively.
So hereMemberwise Initialization is bitwise copy.
IfThe class does not provide an explicit copy construct. When this class object uses another object of the same class as the initial value, do not have a member class object, this member class has a copy constructor (whether explicitly stated by the class designer or synthesized by the compiler, in short it is nontrivial). In this case, this class will not show "bitwise copy Semantics ", that is to say, memberwise Initialization is not performed on the member Class Object recursively. Instead, memberwise Initialization is completed by calling the copy constructor of the member class.
Bitwise copy
To sum up,The precondition for bitwise copy is that the class does not provide an explicit copy construct, which can be understood in two aspects: one is the situation where the member class oject is contained, and the other is the situation where the class is inherited from a base class; of course, we don't need to discuss the case where the class contains virtual functions or virtual base classes, and bitwise copy will certainly not appear.
1. IfClass does not provide an explicit copy construct. When this class object uses another object of the same class as the initial value, if it has a member class object, when initializing a member Class Object, perform the memberwise initialization recursively without calling copy constructor for initialization.
2. IfClass does not provide an explicit copy construct. When this class object uses another object of the same class as the initial value, if it inherits from a base class, the base class does not have a copy constructor (not explicitly declared by the class designer or synthesized by the compiler). The initialization of the base class is similar to initializing the member class object, copy the base class-related member directly to the base class-related member to be initialized.
Memberwise assignment & memberwise Initialization
OneClass object can be obtained in two ways: initialization and assignment to exclude bitwise copy, the main difference between them is that memberwise assignment calls each member assignment operator (operator =), while memberwise initialization calls copy constructor of each member.
See: http://hi.baidu.com/hongszh/blog/item/c312c4ca63c2584ef21fe797.html