Differentiate when to call constructor, copy constructor and value assignment operator example

Source: Internet
Author: User

# Include <iostream>
# Include <vector>
Using namespace STD;
Struct exmp1 {
// Default constructor
Exmp1 ()
{
Cout <"exmp1 ()" <Endl;
}
// Copy the constructor
Exmp1 (const exmp1 &)
{
Cout <"exmp1 (const exmp1 &)" <Endl;
}
// Value assignment operator
Exmp1 & operator = (const exmp1 & RHE)
{
Cout <"operator = (const exmp1 &)" <Endl;
Return * this;
}
// Destructor
~ Exmp1 ()
{
Cout <"~ Exmp1 () "<Endl;
}
};
Void func1 (exmp1 OBJ) // The parameter is an exmp1 object.
{
}
Void func2 (exmp1 & OBJ) // reference of an exmp1 object
{
}
Exmp1 func3 ()
{
Exmp1 OBJ;
Return OBJ; // return the exmp1 object
}
Int main ()
{
Exmp1 eobj; // call the default constructor to create the exmp1 object eobj;
Func1 (eobj); // call the copy constructor.
// Create an exmp1 object as a copy of an exmp1 object
// Call the destructor to cancel the exmp1 object after the function is executed.
 
Func2 (eobj); // reference an exmp1 object. You do not need to call the copy constructor to pass the real parameter.

Eobj = func3 (); // call the default constructor to create a local exmp1 object
// Call the copy constructor to create an exmp1 object as the return value copy when the function returns
// Call the destructor to cancel the partial exmp1 object.
// Call the value assignment operator
// After the value assignment operation is completed, call the destructor to cancel the exmp1 object as the copy of the returned value.

Exmp1 * P = new exmp1; // call the default constructor to dynamically create an eamp1 object

Vector <exmp1> evec (3); // call the default constructor to create a temporary object value (eamp1 ).
// Call the copy constructor three times to copy the temporary value eamp1 object to each element of the evec of the vector container.
// Then call the destructor to cancel the temporary value eamp1 object

Delete P; // call the destructor to cancel the dynamically created eamp1 object

Return 0; // when the lifecycle of evec and eobj ends, the Destructor is automatically called to undo it.
// Call the Destructor three times to cancel evec
}

Running result:

Exmp1 ()
Exmp1 (const exmp1 &)
~ Exmp1 ()
Exmp1 ()
Exmp1 (const exmp1 &)
~ Exmp1 ()
Operator = (const exmp1 &)
~ Exmp1 ()
Exmp1 ()
Exmp1 ()
Exmp1 (const exmp1 &)
Exmp1 (const exmp1 &)
Exmp1 (const exmp1 &)
~ Exmp1 ()
~ Exmp1 ()
~ Exmp1 ()
~ Exmp1 ()
~ Exmp1 ()
~ Exmp1 ()

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.