Copy the constructor and assign values to the constructor.

Source: Internet
Author: User

Copy the constructor and assign values to the constructor.

1 class Widget {2 3 widgets (); // default constructor 4 5 widgets (const Widget & rhs ); // copy the constructor 6 7 Widget & operator = (const Widget & rhs); // value assignment operation function 8 9}; 10 Widget w1; // call the default constructor 11 Widget w2 (w1); // call the copy constructor 12 w1 = w2; // call the value assignment operation function

The preceding statement is easy to understand, but we need to note that "=" can also be used to call the copy constructor.

Widget w3 = w2;

At this time, a copy constructor is called. If a new object is defined, a constructor is called. For example, the preceding w3 cannot call the value assignment operation. If no new object is defined (for example, w1 = w2 above), no constructor is defined, then the value assignment operation is called.


How does C ++ use the default copy constructor?

This is because the class has a default value assignment operator. Although you haven't written it, the compiler has automatically generated it for you, probably as follows:
A & operator = (A &)
{
This-> XXX = a. XXX; // copy the member variable values of a to the member variable of this object.
....
Return * this;
}

Ps. If your class contains pointer member variables, you must rewrite the copy constructors and value assignment operators. If you do not override it, the default copy constructor and value assignment operator will be called, while the default one is the bitwise copy of member variables, therefore, the pointer variables of two class objects point to a memory at the same time.

The following is a classic interview question about this question. In this case, you must rewrite the copy constructor and the value assignment operator. You can write it by yourself, which is very helpful for you to understand it.

The prototype of the known class String is:
Class String
{
Public:
String (const char * str = NULL); // common Constructor
String (const String & other); // copy the constructor
~ String (void); // destructor
String & operate = (const String & other); // value assignment function
Private:
Char * m_data; // used to save strings
};
Compile the preceding four functions of String.

The difference between the c ++ copy constructor and the value assignment operator overload function is that

The copy constructor accepts a reference parameter of a single class type. This parameter is generally modified using const.
Class
{Public:
A ();;
A (const &);
//.........
};
For a general class, the constructor synthesized by the compiler can complete the necessary work. Assume that a data member is a pointer or a member represents other resources allocated in the constructor. You must do some specific work when creating a new object. In both cases, the replication constructor must be defined.

Duplicate operator overloading. operator symbols defined after operator are defined to define the value assignment by defining the name operator = function. This operator function has two parameters: the first parameter corresponds to the operand on the left (implicitly bound to the this pointer), and the second parameter corresponds to the right operand. The return type should be the same as that returned by the built-in value assignment operation. The built-in value assignment operation returns a reference to the right operand, and the value assignment operator returns a reference to the same type.
Class B
{
Public:
B & operator = (const B &);
};

Classes that can use the composite copy constructor can also use the composite value assignment operator. Generally, if the class needs to copy the constructor, a value assignment operator is required.

OK ......

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.