Analysis of Copy constructors for C + + class objects

Source: Internet
Author: User
Tags constructor shallow copy

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

int a=100;
int b=a;

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

#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. From the results of the above code, we can see that the system allocates memory for object B and completes the process of copying with object A. In the case of class objects, class objects of the same type are copied from the constructor to complete the entire replication process. Here's an example of how the copy constructor works.

#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 you can see, the copy constructor is a special constructor, the name of the function must be the same as the class name, and its only one argument is a reference variable of this type, which is a const type, immutable. For example: the copy constructor of Class X is in the form of X (x& x).

The copy constructor is invoked automatically when a newly constructed object is initialized with an initialized custom class type object to initialize. That is, the copy constructor will be invoked when the object of the class needs to be copied. Copy constructors are called in the following situations:

An object is passed in as a value to the function body

An object is returned from a function in a value-passing way

An object needs to be initialized with another object.

If you do not explicitly declare a copy constructor in a class, the compiler will automatically generate a default copy constructor that completes a bit copy between objects. A bit copy is also known as a shallow copy, which is described later.

A custom copy constructor is a good programming style that prevents the compiler from forming a default copy constructor that increases the efficiency of the source code.

Shallow copy and deep copy

In some cases, a member variable in a class needs to dynamically open up heap memory, and if a bit copy is implemented, the value in 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 memory. This is the problem: when B releases the memory (e.g., destructor), the pointer in A is a wild pointer, and a run-time error occurs.

Deep copy and shallow copy can be easily understood as: If a class has resources, when the object of this class occurs when the replication process, the resource reallocation, the process is a deep copy, conversely, no reallocation of resources, is a shallow copy. Here's 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;
}

Well, that's all, I hope this article will help you.

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.