Analysis of Copy constructors for C + + class objects

Source: Internet
Author: User

For normal types of objects, replication between them is simple, for example:

int a=100;
int b=a;

And the class object is different from ordinary object, the inner structure of class object is generally more complex, there are various member variables. Let's look at a simple example of a class object copy.

#include <iostream>
using namespace std;
class CA
{
 public:
  CA(int b)
  {
   a=b;
  }
  void Show ()
  {
   cout<<a<<endl;
  }
 private:
  int a;
};
int main()
{
 CA A(100);
 CA B=A;
 B.Show ();
 return 0;
}

Run the program, screen output 100. As can be seen from the running results of the above code, the system allocates memory for object B and completes the copy process with object A. In the case of a class object, a class object of the same type is done by copying the constructor to complete the entire copy process. Below we illustrate the working process of copy constructors.

#include <iostream>
using namespace std;
class CA
{
 public:
  CA(int b)
  {
   a=b;
  }
  CA(const CA& C)
  {
   a=C.a;
  }
  void Show()
  {
   cout<<a<<endl;
  }
 private:
  int a;
};
int main()
{
 CA A(100);
 CA B=A;
 B.Show ();
 return 0;
}

The CA (const ca& C) is our custom copy constructor. As can be seen, the copy constructor is a special constructor, the name of the function must be the same as the class name, and its only parameter is a reference variable of this type, which is a const type and immutable. For example, the copy constructor for Class X is in the form of X (x& x).

When you initialize another newly constructed object with a custom class type Object that has already been initialized, the copy constructor is automatically called. In other words, the copy constructor is called when the object of the class needs to be copied. The copy constructor is called in the following cases:

An object passed into the function body as a value

An object is returned from the function in the way that the value is passed

An object needs to be initialized with another object.

If a copy constructor is not explicitly declared in the class, the compiler will automatically generate a default copy constructor that completes the bit copy between the objects. A bit copy is also called a shallow copy, which is explained later.

Custom copy constructors are a good programming style that prevents the compiler from forming a default copy constructor, which improves the efficiency of the source code.

Shallow copy and deep copy

In some cases, in-class member variables need to dynamically open up heap memory, if a bit copy is implemented, that is, the value of the object is completely copied to another object, such as A=b. At this point, if a member variable pointer in B has already applied for memory, that member variable in a also points to the same piece of memory. There is a problem: when B releases the memory (for example, a destructor), a pointer inside a is a wild pointer, and a run error occurs.

Deep copy and shallow copy can be simply understood as: If a class has resources, when the object of this class has a replication process, the resource is redistributed, the process is a deep copy, conversely, no redistribution of resources, is a shallow copy. Here is an example of a deep copy.

#include <iostream>
using namespace std;
class CA
{
 public:
  CA(int b,char* cstr)
  {
   a=b;
   str=new char[b];
   strcpy(str,cstr);
  }
  CA(const CA& C)
  {
   a=C.a;
   str=new char[a]; //深拷贝
   if(str!=0)
    strcpy(str,C.str);
  }
  void Show()
  {
   cout<<str<<endl;
  }
  ~CA()
  {
   delete str;
  }
 private:
  int a;
  char *str;
};
int main()
{
 CA A(10,"Hello!");
 CA B=A;
 B.Show();
 return 0;
}

http://www.lwinfo.com/uzt/list1/207177.html
http://www.lwinfo.com/uzt/list1/207672.html
http://www.lwinfo.com/uzt/list1/207673.html
http://www.lwinfo.com/uzt/list1/207675.html
http://www.lwinfo.com/uzt/list1/207677.html
http://www.lwinfo.com/uzt/list1/207678.html
http://www.lwinfo.com/uzt/list1/207679.html
http://www.lwinfo.com/uzt/list1/207680.html
http://www.lwinfo.com/uzt/list1/207682.html
http://www.lwinfo.com/uzt/list1/207684.html
http://www.lwinfo.com/uzt/list1/207686.html
http://www.lwinfo.com/uzt/list1/207689.html
http://www.lwinfo.com/uzt/list1/207690.html
http://www.lwinfo.com/uzt/list1/207693.html
http://www.lwinfo.com/uzt/list1/207695.html
http://www.lwinfo.com/uzt/list1/207696.html
http://www.lwinfo.com/uzt/list1/207698.html
http://www.lwinfo.com/uzt/list1/207701.html
http://www.lwinfo.com/uzt/list1/207702.html
http://www.lwinfo.com/uzt/list1/207706.html
http://www.lwinfo.com/uzt/list1/207707.html
http://user.qzone.qq.com/3055716359
Http://t.qq.com/wfnpxyiyuan
http://user.qzone.qq.com/3055716359/blog/1429063798

Analysis of Copy constructors for C + + class objects

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.