Copy constructor in C++

 A question has been bugging me some time ago - what is a constructor? ? What is a copy constructor again? ?

Because Xiaobo doesn't often use copy constructors and the like in his work, and he belongs to the type that only seeks answers when encountering problems, so. . . (a bit of nonsense)

------------------------gorgeous. . gorgeous. . . . It's a gorgeous dividing line-------------------

I won't explain the constructor here. Let's look at the copy constructor directly. In fact, if you really understand its principle, then your first thought is. . . I rely on, is that so? ?

First of all, from the name, the copy constructor (well, it sounds tall), can actually be divided into two --- copy, constructor

To put it bluntly, when the function is constructed,

Assign other classes to the whole

code below

#include <iostream>
using namespace std;

class productA { public: productA();//This is the constructor productA(productA& item);//This is the copy constructor private: int a; }; //Constructor, assign initial value productA::productA() { this->a = 1; } //Yes, yes, this is a copy constructor productA::product(product* item) { this->a = item->a; } //accomplish int main(int argc, char* argv[]) { productA* A = new productA(); productA* B = new productA(A); return 0; }

  -------------------------Being too lazy to describe the dividing line-------------------- ---------

If you have any objection to this, please leave a message, exchange and discuss, Xiaobo is very grateful!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325121498&siteId=291194637