C++ class copy assignment destruction of the example

  • 2020-05-30 20:51:44
  • OfStack

Examples of copy, assignment, and destruction of C++ classes

In this article, we will explain a few knowledge points in 1:

Class copy constructor.

Class copy assignment operator.

Class.

Okay, one by one

If we don't define a copy constructor for our class, the compiler will synthesize the default copy constructor for us -- the synthetic copy constructor.

And into the operation of the copy constructor is to copy all the members of its parameters to the object is created, each member's type determines how he copied: members of class type, will use the copy constructor, members of built-in type are copied directly, although we cannot copy an array directly, but the synthesized copy constructor would literally copy an array type of member.

Let's demonstrate the functionality of the synthetic copy constructor in code 1 below:


#include <iostream> 
using namespace std; 
/* 
   The code mimics the functionality of the synthetic copy constructor  
*/ 
class Sales_data 
{ 
public: 
  Sales_data(); 
  // We just restored it for the reader to understand better 1 The following composite copy constructor implements functions that are virtually invisible to us  
  Sales_data(const Sales_data& s); 
  ~Sales_data(); 
 
private: 
  string bookNo; 
  int units_sold = 0; 
  double revenue = 0.0; 
 
}; 
 
Sales_data::Sales_data() 
{ 
} 
 
Sales_data::Sales_data(const Sales_data& s) : 
bookNo(s.bookNo), units_sold(s.units_sold), revenue(s.revenue){ 
   
} 
 
Sales_data::~Sales_data() 
{ 
} 

Ok, so let's define the copy constructor and define it that way, and let me explain why there is a static reference in the parameter table:

1) we don't want to change the value of the reference, just for copying.

2) we must declare it as a reference, and if we write it as a class, we will call the copy constructor of that class again, and within that copy constructor we will call the copy constructor again, so we are stuck in a loop.

Let's learn about the 1 copy assignment operator:

Like copy constructor 1, if we don't define a copy assignment operator, the compiler will synthesize one for us. Let's use the following code to simulate 1:


#include <iostream> 
using namespace std; 
/* 
 The code mimics the copy assignment operator  
*/ 
class Sales_data 
{ 
public: 
  Sales_data(); 
  // Copy the default composition of the assignment operator  
  Sales_data& operator = (const Sales_data & s); 
  ~Sales_data(); 
 
private: 
  string bookNo; 
  int units_sold = 0; 
  double revenue = 0.0; 
 
}; 
 
Sales_data::Sales_data() 
{ 
} 
 
Sales_data& Sales_data::operator = (const Sales_data & s) { // Why is our return value 1 Must be the reference type? This is because we can do chain-programming: s1 = s2 =s3; 
  bookNo = s.bookNo; 
  units_sold = s.units_sold; 
  revenue = s.revenue; 
  return *this; 
} 
 
Sales_data::~Sales_data() 
{ 
} 

Ok, so let's define the copy assignment operator as well, so we won't go through 11.

I'm here to talk. Class constructor initialization and class destructor destruction process 1 some details.

1 let's say we define a constructor, and that's actually where we do the initialization before we parameterize the assignment list and the curly braces, and all we do in the curly braces is copy the assignment.

We should be aware of this point, otherwise there is a problem in the initialization of const objects, because const objects can only be initialized.

2. For example, if we define a destructor, notice that we have done the real destructor operation of the class member variables in the section between () {, and the custom operation inside {}, no one must be what the destructor is, because the destructor is basically finished.

If you have any questions, please leave a message or come to the site community to exchange discussion, thank you for reading, hope to help you, thank you for your support of the site!


Related articles: