C++ 自定义类型属性的拷贝过程

#include <iostream>
#include <string>
using namespace std;
class P {
   public:
    int id;
    string name;

   public:
    P() { cout << "wu can p gou zao " << endl; }

    P(int id, string name) { cout << "you can p gou zao" << endl; }
    P(const P &p) { cout << "p de 拷贝构造函数" << endl; }
};

class Person {
   public:
    int id2 = 202;
    int id1 = 201;
    P p;

   public:
    Person() { cout << "person gou zao " << endl; }
};

int main(int argc, char const *argv[]) {
    Person p1;
    // 默认情况下,会调用自定义类型属性的拷贝构造函数
    Person p2 = p1;
    return 0;
}
posted @ 2021-11-08 08:54  邱明成  阅读(119)  评论(0编辑  收藏  举报