Compiler synthesis and replication Constructor

Source: Internet
Author: User

Definition:

Only one parameter is specified. this parameter is a reference to an object of this type (commonly used for const modification ).ConstructorBecome a replication constructor.

Usage:

(1) display use ---- when initializing this object with an object of the same type;

(2) Implicit usage ---- when an object of this type is passed to a function or returned from a function.

Three types of replication constructor:

* Bitwise copy constructor: Bit-by-bit copy ----- default mode

* Merged copy constructor: Compiler merging ---- execute member initialization (memberwise initialize) to complete only necessary work,

* Custom copy constructor: defined by the class designer ------ because some classes must control the copy object, if the data member is a pointer or the data member represents other resources allocated in the constructor, or the class must do some specific work when creating a new object, in this case, you need to define your own copy constructor.

For shortest copy and deep copy:

Shortest copy: Shllow copy = bitwise copy ----- one-byte copy (Default)

Example:

[CPP]View plaincopyprint?
  1. <Span style = "font-size: 24px;"> class base {
  2. Public:
  3. Int;
  4. Char ch;
  5. Char * STR;
  6. }
  7. Base B1;
  8. B1.a = 15;
  9. B1.ch = 'C ';
  10. B1.str = "string ";
  11. B2 = B2; </span>

Now let's take a look at the memory layout of B1 and B2 (not considering alignment ):


We can see that b1.str and b2.str point to the same memory space, that is, when one party withdraws, the other party will be affected, so we should try our best to avoid this situation ----- deep copy

Deep copy: Deep copy = memberwise copy ----- copy from one member

Let's look at the memory layout of B1 and B2:


B1.str and b2.str point to different memory spaces, but the content (string) is the same.

The default compiler is bitwise copy semantics.

That is, if the class is not clearly defined or compiled into copy constructor (that is, memberwise copy), bitwise copy is used by default.

Note: The above compilers merge copy constructor and bitwise copy constructor without the participation of class design!


So under what circumstances will bitwise copy semantics become invalid?

YesFour cases:

(1) When the class contains a class member object, and the latter class declaration has a copy constructor, it is explicitly declared by the class designer or synthesized by the compiler ------- recursive call;

(2) When the class inherits from a base class and the latter has a copy constructor, it is explicitly declared by the class designer or synthesized by the compiler ------- recursive call;

(3) When the class declares one or more virtual functions, ------- the value of the virtual function table pointer vptr in the class object should be considered.

When initializing objects at the same Layer, Bitwise copy is enough (To simplify the process, the pointer member is not considered.);

Example:

[CPP]View plaincopyprint?
  1. <Span style = "font-size: 18px;"> class zooanimal {
  2. Public:
  3. Zooanimal ();
  4. Virtual ~ Zooanimal ();
  5. Virtual void animate ();
  6. Virtual void draw ();
  7. .....
  8. };
  9. Class bear: Public zooanimal {
  10. Public:
  11. Bear ();
  12. Void animate ();
  13. Void draw ();
  14. Virtual void Dance ();
  15. ......
  16. };
  17. Bear Yogi;
  18. Bear Winnie = Yogi; // bitwise copy </span>

The memory layout is as follows:

However, if:When a base class object is initialized with its derived class Object contentIn this case, if bitwise copy is normal, because its vptr points to different virtual tables, the compiler needs to synthesize copy constrctor to ensure the security of the vptr copy operation. The merged base class Object copy constructor explicitly sets the vptr of the object to the vitrual table of the base class object, instead of directly copying the current value of the vptr from the class object on the right hand side.

For example:

[CPP]View plaincopyprint?
  1. <Span style = "font-size: 18px;"> void draw (const zooanimal & Zoey)
  2. {
  3. Zoey. Draw ();
  4. }
  5. Zooanimal Franny = Yogi; // sliced
  6. Draw (Yogi); // call Bear: Draw ();
  7. Draw (Franny); // call zooanimal: Draw () </span> <span style = "font-size: 16px;">
  8. </Span>

The memory layout is as follows ( Copy constructor is synthesized by the compiler.):


(4) When the class is derived from an inherited string, one or more virtual base classes are involved.

Compiler synthesis and replication Constructor

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.