C ++/CLI speculative record copy constructor

Source: Internet
Author: User

Although object replication looks simple, if you do not understand it correctly, there may be some serious problems.By default, copying an object will result in the replication of all members. If you only have instance members, it looks pretty good. But what happens if your class contains objects that point to the objects allocated in the heap?Consider the followingCodeParts:

# Include <stdio. h>
# Include <string. h>
Class person
{
PRIVATE:
Char * _ name;
Public:
Person ()
{
_ Name = new char [256];
}
Void setname (const char * name)
{
If (strlen (name) + 1 <256)
Strcpy (_ name, name );
}
Void printname ()
{
Printf ("% s \ n", _ name );
}
};
Int main ()
{
// Create the first instance of the object and assign it to John
Person P1;
P1.setname ("John ");
P1.printname ();
// Create another object by copying the object referenced by P1
Person P2 (P1 );
P2.setname ("Alice ");
P2.printname ();
// Output the P1 name again.
P1.printname ();
Scanf ("Q ");
Return 0;
}

Here, the class person has a pointer to the character array allocated on the stack. When constructing the person object, it creates the character array and stores its location in Variable _ name.

However, when you create the person object P2, P2 members are initialized with P1 members. Therefore, the _ name of P1 and the _ name of P2 point to the same heap object. As shown in the preceding example, calling p2.setname will change the value shared by the two classes. Therefore, when p1.printname is called for the second time, the printed result is "Alice ".

Therefore, this is not the expected result of copying objects, but also causes heap crash. When a function deletes the array and P1 calls the function again? Next, when P2 calls printname, it tries its best to access objects that are not actually on the stack. In this case, the results are often unpredictable.

C ++ allows us to overcome this type of problem by defining the copy constructor. Every time we Initialize an object by copying another object, the copy constructor is executed. You can overwrite the replication behavior of the default member function in the copy constructor.

Therefore, we should modify the class person as follows:

Class person
{
PRIVATE:
Char * _ name;
Public:
Person ()
{
_ Name = new char [256];
}
// This Is A copy constructor. Here we initialize a new array used by the person instance.
Person (person &)
{
_ Name = new char [256];
}
Void setname (const char * name)
{
If (strlen (name) + 1 <256)
Strcpy (_ name, name );
}
Void printname ()
{
Printf ("% s \ n", _ name );
}
};

Here, the copy constructor in the person class ensures that it initializes a new array for each object instance generated during replication. This avoids the problems we mentioned earlier.

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.