C + + Copy constructors

Source: Internet
Author: User

classComplex {Private :    DoubleM_real; DoubleM_imag; Public:     //parameterless Constructors//If you create a class and you do not write any constructors, the system automatically generates the default parameterless constructor, the function is empty, and nothing is done//as long as you write one of the following constructors, the system will no longer automatically generate such a default constructor, if you want to have such a parameterless constructor, you need to write it yourselfComplex (void) {M_real=0.0; M_imag=0.0; }              //General Constructors (also called overloaded constructors)//General constructors can have a variety of parameter forms, a class can have more than one general constructor, if the number of parameters or different types (based on the principle of overloaded functions of C + +)//For example: You can also write a Complex (int num) constructor .//different constructors are called depending on the parameters passed in when the object is createdComplex (DoubleRealDoubleimag) {M_real=Real; M_imag=Imag; }         //Copy Constructors (also known as copy constructors)//A copy constructor parameter is a reference to the class object itself that copies a new object of that class based on an existing object, typically copying the value of the data member of an existing object into the newly created object in the function//If you do not see a write-copy constructor, the system creates a copy constructor by default, but when there are pointer members in the class, there is a risk that the copy constructor is created by default, for specific reasons, please inquire about "shallow copy", "Deep copy" article discussionComplex (ConstComplex &c) {//Copy the value of the data member in Object C.M_real =C.m_real; M_img=c.m_img; }                 //A type conversion constructor that creates an object of this class based on an object of a specified type//For example: The following will create a complex object based on an object of type doubleComplex::complex (Doubler) {m_real=R; M_imag=0.0; }     //equals operator overload//Note that this is similar to the copy constructor, which copies the value of the object of this class = Right to the object to the left of the equals sign, which is not a constructor, and the object on both sides of the equal sign must have been created//if the Write = operator overload is not displayed, the system also creates a default = operator overload, doing only basic copy workComplex &operator=(ConstComplex &RHS) {        //first detect whether the right side of the equal sign is the object of the left, if the object itself, the direct return        if( This= = &RHS) {            return* This; }                     //copy the member to the right of the equal sign to the left object         This->m_real =Rhs.m_real;  This->m_imag =Rhs.m_imag; //send the object to the left of the equal sign again//The purpose is to support the EG:A=B=C system to run first B=c//then run a= (the return value of B=c, which should be the B object after the C value is copied)        return* This; }};

C + + Copy constructors

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.