【C++】类和对象之拷贝构造函数篇
- 软件开发
- 2025-08-03 16:21:01

个人主页 : zxctscl 文章封面来自:艺术家–贤海林 如有转载请先通知
文章目录 1. 前言2. 传值传参和传引用传参3. 概念4. 特征 1. 前言在前面学习了6个默认成员函数中的构造函数和析构函数 【C++】构造函数和析构函数详解,接下来继续往后看拷贝构造函数。
拷贝构造函数就是用一个同类型的其他对象来构造。 要学习拷贝构造函数,得先了解传值传参和传引用传参。
2. 传值传参和传引用传参 class Date { public: Date(int year = 1, int month = 1, int day = 1) { _year = year; _month = month; _day = day; } Date(Date& d) { _year = d._year; _month = d._month; _day = d._day; } void Print() { cout << _year << "-" << _month << "-" << _day << endl; } private: // 内置类型 int _year; int _month; int _day; }; void func1(Date d) { } void func2(Date& rd) { } int main() { Date d1(2024, 2, 24); func1(d1); func2(d1); return 0; }C++规定自定义类型都会调用拷贝构造。 所以func2(d1);rd是d1的别名,直接完成调用了,不存在再有一个函数来传值传参。 来看看不传引用会怎么样
class Date { public: Date(int year = 1, int month = 1, int day = 1) { _year = year; _month = month; _day = day; } Date(Date d) { _year = d._year; _month = d._month; _day = d._day; } void Print() { cout << _year << "-" << _month << "-" << _day << endl; } private: int _year; int _month; int _day; }; int main() { Date d1(2024, 2, 24); Date d2(d1); return 0; }调用拷贝构造,要先传参,这里是传值,传参,会形成一个新的拷贝构造,进不来这个函数,一直递归下去。使用传值传参就不行。
3. 概念在现实生活中,可能存在一个与你一样的自己,我们称其为双胞胎。
那在创建对象时,可否创建一个与已存在对象一某一样的新对象呢? 拷贝构造函数:只有单个形参,该形参是对本类类型对象的引用(一般常用const修饰),在用已存在的类类型对象创建新对象时由编译器自动调用。
4. 特征拷贝构造函数也是特殊的成员函数,其特征如下:
拷贝构造函数是构造函数的一个重载形式。拷贝构造函数的参数只有一个且必须是类类型对象的引用,使用传值方式编译器直接报错,因为会引发无穷递归调用。有时候可能会发生修改对象,为了保护对象,就可以在它前面加上const。 是一种权限的缩小。
像下面的场景就能被检查出来: 就只能这样写:
若未显式定义,编译器会生成默认的拷贝构造函数。 默认的拷贝构造函数对象按内置类型成员内存存储按字节序完成拷贝,这种拷贝叫做浅拷贝,或者值拷贝。像下面的代码中就没有拷贝构造:
class Date { public: Date(int year = 1, int month = 1, int day = 1) { _year = year; _month = month; _day = day; } void Print() { cout << _year << "-" << _month << "-" << _day << endl; } private: // 内置类型 int _year; int _month; int _day; }; int main() { Date d1(2024, 2, 24); Date d2(d1); d1.Print(); d2.Print(); return 0; } class Time { public: ~Time() { cout << "~Time()" << endl; } // 强制编译器生成 Time() = default; Time(const Time& t) { cout << "Time(const Time& t)" << endl; _hour = t._hour; _minute = t._minute; _second = t._second; } private: int _hour; int _minute; int _second; }; class Date { public: Date(int year = 1, int month = 1, int day = 1) { _year = year; _month = month; _day = day; } void Print() { cout << _year << "-" << _month << "-" << _day << endl; } private: // 内置类型 int _year; int _month; int _day; //自定义类型 Time _t; }; int main() { Date d1(2024, 2, 24); Date d2(d1); d1.Print(); d2.Print(); return 0; }在编译器生成的默认拷贝构造函数中,内置类型是按照字节方式直接拷贝的,而自定 义类型是调用其拷贝构造函数完成拷贝的。
编译器生成的默认拷贝构造函数已经可以完成字节序的值拷贝了,还需要自己显式实现吗? typedef int DataType; class Stack { public: Stack(size_t capacity = 10) { _array = (DataType*)malloc(capacity * sizeof(DataType)); if (nullptr == _array) { perror("malloc申请空间失败"); return; } _size = 0; _capacity = capacity; } // Stack st2(st1); Stack(const Stack& s) { DataType* tmp = (DataType*)malloc(s._capacity *(sizeof(DataType))); if (tmp == nullptr) { perror("malloc fail"); exit(-1); } memcpy(tmp, s._array, sizeof(DataType) * s._size); _array = tmp; _size = s._size; _capacity = s._capacity; } void Push(const DataType& data) { // CheckCapacity(); _array[_size] = data; _size++; } ~Stack() { if (_array) { free(_array); _array = nullptr; _capacity = 0; _size = 0; } } private: DataType* _array; size_t _size; size_t _capacity; }; int main() { Stack s1; s1.Push(1); s1.Push(2); s1.Push(3); s1.Push(4); Stack s2(s1); return 0; }看这里s1和s2置空,s1置空不影响s2置空,他们是两个对象。但他们的地址指向同一个空间,同一个空间不能释放两次。会导致野指针。 注意:类中如果没有涉及资源申请时,拷贝构造函数是否写都可以;一旦涉及到资源申请时,则拷贝构造函数是一定要写的,否则就是浅拷贝。
拷贝构造函数典型调用场景: 使用已存在对象创建新对象 函数参数类型为类类型对象 函数返回值类型为类类型对象 class Date { public: Date(int year, int minute, int day) { cout << "Date(int,int,int):" << this << endl; } Date(const Date& d) { cout << "Date(const Date& d):" << this << endl; } ~Date() { cout << "~Date():" << this << endl; } private: int _year; int _month; int _day; }; Date Test(Date d) { Date temp(d); return temp; } int main() { Date d1(2022, 1, 13); Test(d1); return 0; }为了提高程序效率,一般对象传参时,尽量使用引用类型,返回时根据实际场景,能用引用尽量使用引用。
有问题请指出,大家一起进步!!!
【C++】类和对象之拷贝构造函数篇由讯客互联软件开发栏目发布,感谢您对讯客互联的认可,以及对我们原创作品以及文章的青睐,非常欢迎各位朋友分享到个人网站或者朋友圈,但转载请说明文章出处“【C++】类和对象之拷贝构造函数篇”
上一篇
Python习题详解