1. 拷贝构造函数和赋值运算符重载区别

1.1. 拷贝构造函数用于构造新的对象

Student s;
Student s1 = s; // 隐式调用拷贝构造函数
Student s2(s);  // 显式调用拷贝构造函数

1.2. 赋值运算符重载用于将源对象的内容拷贝到目标对象中,而且若源对象中包含未释放的内存需要先将其释放

Student s;
Student s1;
s1 = s; // 使用赋值运算符

一般情况下,类中包含指针变量时需要重载拷贝构造函数、赋值运算符和析构函数。

Copyright © ershouche-FE 2019 all right reserved,powered by Gitbook文件修订时间: 2022-02-28 16:12:18

results matching ""

    No results matching ""