C ++ default copy constructor memo

Source: Internet
Author: User
Tags class operator

At the beginning of chapter 9 "high quality programming guide _ Lin Rui,

 

"The default copy constructor and the default value assignment function are both implemented by 'bit copy' instead of 'value copy'. If the class contains pointer variables, these two functions are doomed to go wrong"

 

What attracts my attention is the word "bit copy". e seems to be "bitwise copy", full name "bit by bit copy ". I have not read the original e-text version.

 

To be honest, it is very irresponsible to throw out a conceptual object without a certain explanation.

 

Some people on the Internet say that 'bit copy' is 'shortest copy', and the copy is the address; 'value copy' should be 'deep copy', and the copy is the content.

 

------------------------------------------------- Cute split line -------------------------------------------------------------

 

In fact, the above are all nonsense. The last sentence at the beginning of chapter 1 <copy control> in <C ++ primer version 4> is:

 

There is a special common situation that requires the class to define its own replication control members: the class has pointer members.(Copy constructor. The value assignment operator and destructor are called copy control)

 

In 13.1.1 merged copy constructor, the merged copy constructor is the default copy constructor. Its behavior is to executeInitialize members one by one (menberwise initialize ),Initialize the new object as a copy of the original object.

 

The so-called "initialize members one by one" means that the compiler copies each non-static member of an existing object to the newly created object in sequence. there is only one exception. The type of each member determines the meaning of the member to be copied. the composite replication constructor directly copies the values of built-in type members. class members use the replication constructor of this class to copy the values. the replication of array members is an exception. Although arrays cannot be copied, if a class has an array Member, The compositing replication constructor will copy the array. when copying an array, the synthesis and replication constructor copies each element of the array.

 

For example, class classa

{

.........

PRIVATE:

Int IA;
Int * pb;

}

 

Then, the synthesis copy transformation function is as follows:

Classa: classa (const classa & orig ):

Ia (orig. Ia), Pb (orig. Pb)

{}

 

// Write a classb with its own copy constructor, A classc. One of its member variables is the classb object, classc C; classc D (c); facts prove everything

 

See Pb (orig. PB). The pointer Pb is the pointer orig. A copy of Pb, But they point to the same. * pb and * orig. PB points to the same resource, and any change between them will affect each other. this is the case for pointers. Therefore, only one sentence is written at the beginning of chapter 13 of <C ++ primer version 4>.

 

What 'bit copy' and 'value copy' are more like a mixture of our thoughts.

 

In my opinion, if you still want to split 'bit copy' or 'value copy', this member variable is 'value copy', whether it is a built-in type, class type or pointer, their addresses are different from the copied objects, proving that they are all allocated memory space. the pointer member variable points to another address.

 

The test example is very simple.

 

 

 

 

 

 

------------------------------------------------- Cute split line -------------------------------------------------------------

<C ++ primer version 4> section 15.4.3

 

 

1. If the derived class defines its own copy constructor, the copy constructor should explicitly use the base class copy constructor to initialize the base class part of the object:

 

Class base {/*..............*/};

 

Class derived: public base {

Public:

// Base: Base (const base &) not invoked automatically

Derived (const derived & D ):

Base (d)/* Other Member initialization */

{/*..............................*/}

};

 

The initialization function base (d) converts the derived class Object D to a reference of its base class and calls the base class copy constructor. If the base class initialization function is omitted, the following code:

 

Derived (const derived & D)

{/*..............................*/}

 

The result is to run the base.Default constructorInitialize the base class of the object. assuming that the initialization of the derived member copies the corresponding member from D, the newly constructed object will have a strange configuration: its base part will save the default value, its Derived member is a copy of another object.

 

 

 

2. The value assignment operator is usually similar to the copy constructor: If a derived class defines its own value assignment operator, the operator must explicitly assign values to the base class:

 

// Base: Operator = (const base &) not invoked automatically

Derived & derived: Operator = (const derived & RHs)

{

If (this! = RHs ){

Base: Operator = (RHs );// Assigns the base part

................................

}

 

Return * this;

}

The value assignment operator must prevent its own value assignment. if the left and right operands are different, the base class assignment operator is called to assign values to the base class. this operator can be defined by a class or a synthetic value assignment operator. ----------- we can call it directly. the base class operator releases the value of the base class in the left operand and assigns a new value from RHS. after the operator is executed, You need to assign values to the members in the derived class.

 

 

 

3. the work of the Destructor is different from that of the copy constructor and the value assignment operator: The derived class destructor are not responsible for revoking the member of the base class object. the compiler always explicitly calls the destructor of the base class of the derived class object. each destructor is only responsible for clearing its own members:

 

// Base ::~ Base () invoked automatically

Derived ::~ Derived () {/* do what it takes to clean up derived members */}

 

The revocation order of the object is opposite to that of the constructor: first run the derived class destructor, and then call the base class destructor at the inheritance level.

 

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.