Copy constructors for C + + replication control

Source: Internet
Author: User

C + + classes use three special member functions: Copy constructors, assignment operators, and destructors to determine what happens when initializing or assigning a value between class objects. The so-called "replication Control" is the process of controlling the copying of objects through these three member functions. This article describes the copy constructor.

    • Copy constructor

What is a copy constructor

The copy constructor is first a constructor that has the same name as all other constructors, with no return value. It has a unique argument, which is a reference to the class type (which is typically declared as const, derived from the object that is used to assign the value generally without changing its own value). So the prototype of the copy constructor is:

classbook{ Public: Book (Constbook& RHS);//Constructor OneBook (string&name,floatPrice =0): _bookname (name), _price (price) {};//Constructor TwoBook (): _price (0), _bookname (""){};//Constructor Three

   Private:float_price;string_bookname; //....};

When is it called?

The copy constructor is called when a class object needs to be copied, which can be summarized as:

    1. Displays or implicitly initializes an object based on an object of the same type.

When a new object is defined and initialized with an object of the same type, the copy constructor is used explicitly, such as:

Book Book1; Book Book2 (BOOK1);

When you pass an object of that type to a function or return an object of that type from a function, the copy constructor is called implicitly.

    1. An argument passed as a value is passed to a function.
    2. Copies an object when the function returns.
    3. Initializes the elements in the sequential container.

such as:

    vector<string> Svec (5);

The compiler first calls the string class default constructor to create a temporary value, and then copies the temporary values to each element with the copy constructor.

    1. Initializes an array element based on the element initialization list.

Such as:

Book books[]={  string("book1"),  string(" Book2 " ),  string("book3"), book ()};

the first three elements of the book array call the constructor two for implicit type conversion (c + + implicit type conversion), and then call the copy constructor to copy the array elements. If the class prohibits implicit type conversions (the constructor uses the explicit Declaration), or if you want to not specify an argument or multiple arguments, you need to use the full constructor syntax, such as the initialization of the last element of the array.

What happens if the copy constructor is not declared for a class

If you do not declare a copy constructor, then the compiler will give you a declaration. In fact, if you do not declare yourself, the compiler declares a copy constructor for the class , an assignment operator, and a destructor, and if you do not declare any constructors, the compiler will declare a composite default constructor for you. All of these compiler auto-generated class member functions are pubilc and inline. (This section can refer to the "effective C + +" clause ) the copy constructor created by the compiler simply copies each non-static member of the source object to the target object. This is often not enough to meet the class requirements, especially when the class contains pointers, this time we need to write the copy control of the three special member functions.

What the compiler composition's copy constructor does

The behavior of the synthetic copy constructor is to initialize each non- static member individually. Different types of members are initialized differently:

Built-in type (such as int): copies values directly.

Class Type: Call the copy constructor of the class to replicate.

Arrays: This is special because we know that it is generally not possible to copy an array, but in a class, when you copy an array, the composite copy constructor copies each value of the array.

In addition, the initialization of a class data member by the synthetic copy constructor is placed in the constructor initialization list.

Prohibit copying

if we want to prohibit the replication behavior of a class, we certainly don't want to define a copy constructor, but the compiler automatically defines one for us, so how do we stop the replication behavior of a class?

We can define a copy constructor as private, do not allow user code to copy objects of that class type, and if replication occurs, an error will occur at compile time. However, friends and members of the class can still replicate, and the workaround is that we can declare a private copy constructor without definition, and when a class member or friend makes a copy attempt, an error occurs while the program is running.

Summary: In order to dismiss the function provided automatically by the compiler, the corresponding member function can be declared private and not implemented. (specifically, refer to theeffective C + +clause If you do not want to use the compiler automatically generated functions, it should be explicitly rejected)

Copy constructors for C + + replication control

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.