小编Vis*_*rya的帖子

为什么赋值运算符重载会创建对象的副本?

在下面给出的代码中,我cout在所有类构造函数、析构函数和重载赋值运算符中编写了语句。

#include <iostream>
using namespace std;

class person {
    string name;
    int age ;
    int id ;
    static int num;
public :
    person (string name , int age) : name(name) , age(age) {
        id = num++;
        cout << "creating person : " << id << "(" << name <<")"<< endl;
    }
    person (const person &other) : name(other.name) , age(other.age) {
            id = num++;
            cout << "CREATING PERSON  : " << id << "(" << name <<")" << " …
Run Code Online (Sandbox Code Playgroud)

c++ class operator-overloading copy-constructor

2
推荐指数
1
解决办法
150
查看次数