C++中拷贝构造函数的使用

 更新时间:2022年02月14日 14:40:49   作者:骆驼胡杨  
大家好,本篇文章主要讲的是C++中拷贝构造函数的使用,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下

拷贝构造函数

拷贝构造函数,它只有一个参数,参数类型是本类的引用。
复制构造函数的参数可以是 const 引用,也可以是非 const 引用。 一般使用前者,这样既能以常量对象(初始化后值不能改变的对象)作为参数,也能以非常量对象作为参数去初始化其他对象。一个类中写两个复制构造函数,一个的参数是 const 引用,另一个的参数是非 const 引用,也是可以的。

1. 手动定义的拷贝构造函数

Human.h

#pragma once
#include <iostream>
#include <Windows.h>
#include <string>
using namespace std;

class Human {
public:		
	Human();
	Human(int age, string name, string sex);
	//手动定义了一个拷贝构造函数
	Human(const Human &other);

	string getName() const;
	string getSex() const;
	int getAge() const;
	void description() const;	//描述信息
private:		
	string name;	//姓名
	string sex;		//性别
	int age;		//年龄
};

Human.cpp

#include "Human.h"

Human::Human() {
	
}

Human::Human(int age, string name, string sex) {
	this->name = name;
	this->sex = sex;
	this->age = age;
}

//拷贝构造函数
Human::Human(const Human& other){
	//把other对象的数据拷贝到另一个对象的私有数据
	this->name = other.name;
	this->sex = other.sex;
	this->age = other.age;
}

string Human::getName() const {
	return name;
}

string Human::getSex() const {
	return sex;
}

int Human::getAge() const {
	return age;
}

void Human::description() const {
	cout << "姓名: " << getName() << endl;
	cout << "年龄: " << getAge() << endl;
	cout << "性别: " << getSex() << endl;
}

main.cpp

#include "Human.h"

Human::Human() {
	
}

Human::Human(int age, string name, string sex) {
	this->name = name;
	this->sex = sex;
	this->age = age;
}

//拷贝构造函数
Human::Human(const Human& other){
	//把other对象的数据拷贝到另一个对象的私有数据
	this->name = other.name;
	this->sex = other.sex;
	this->age = other.age;
}

string Human::getName() const {
	return name;
}

string Human::getSex() const {
	return sex;
}

int Human::getAge() const {
	return age;
}

void Human::description() const {
	cout << "姓名: " << getName() << endl;
	cout << "年龄: " << getAge() << endl;
	cout << "性别: " << getSex() << endl;
}

2. 合成的拷贝构造函数

当程序员没有定义拷贝构造函数时, 编译器会自动生成合成的拷贝构造函数

说明:
合成的拷贝构造函数的缺点: 使用“浅拷贝”

在这里插入图片描述

解决方案:在自定义的拷贝构造函数中,使用‘深拷贝

Human.h

#pragma once
#include <string>
#include <iostream>
#include <Windows.h>
using namespace std;

class Human {
public:		
	Human();

	//定义了一个拷贝构造函数
	Human(const Human & man);

	string getName() const;
	string getSex() const;
	int getAge() const;
	const char* getAddr();
	void setAddr(char* addr);	//设置地址

private:		
	string name;	//姓名
	string sex;		//性别
	int age;			//年龄
	char* addr;		//地址
};

Human.cpp

#include "Human.h"
#define		ADDR_LEN		64

Human::Human() {
	name = "无名";
	sex = "未知";
	age = 18;

	const char* addr_s = "China";
	addr = new char[ADDR_LEN];
	strcpy_s(addr, ADDR_LEN, addr_s);
}

//拷贝构造函数
Human::Human(const Human& other){
	cout << "调用拷贝构造函数" << endl;
	//把other对象的数据拷贝到私有数据
	this->name = other.name;
	this->sex = other.sex;
	this->age = other.age;

	//使用深拷贝, 单独分配一个内存
	this->addr = new char[ADDR_LEN];
	strcpy_s(this->addr, ADDR_LEN, other.addr);
}

string Human::getName() const {
	return name;
}

string Human::getSex() const {
	return sex;
}

int Human::getAge() const {
	return age;
}

const char* Human::getAddr(){
	return addr;
}

void Human::setAddr(char* addr){
	if (!addr) 	return;
	strcpy_s(this->addr, ADDR_LEN, addr);
}
#include "Human.h"
using namespace std;

int main(void) {
	Human zhangsan;
	
	//初始化调用拷贝构造函数
	Human lisi = zhangsan;	//自动调用拷贝构造函数

	//赋值的时候调用的是赋值构造函数
	//lisi = zhangsan;

	cout <<"李四地址: " << lisi.getAddr() << endl;
	cout <<"张三地址: " << zhangsan.getAddr() << endl;

	cout << "张三修改地址" << endl;
	zhangsan.setAddr((char*)"美国");

	cout << "李四地址: " << lisi.getAddr() << endl;
	cout << "张三地址: " << zhangsan.getAddr() << endl;

	system("pause");
	return 0;
}

总结

到此这篇关于C++中拷贝构造函数的使用的文章就介绍到这了,更多相关C++拷贝构造函数内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • c++下迭代器总结

    c++下迭代器总结

    大家好,本篇文章主要讲的是c++下迭代器总结,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下,方便下次浏览
    2021-12-12
  • C语言的变量与常量 字符字符串与转义字符详解

    C语言的变量与常量 字符字符串与转义字符详解

    这篇文章主要介绍了详解C语言的变量与常量 字符字符串与转义字符,包括其之间的区别是C语言入门学习中的基础知识,需要的朋友可以参考下
    2021-10-10
  • 深入理解C预处理器

    深入理解C预处理器

    下面小编就为大家带来一篇深入理解C预处理器。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-08-08
  • C++实现递归函数的方法

    C++实现递归函数的方法

    在本篇内容里小编给大家分享了关于C++实现递归函数的教学步骤,需要的朋友跟着参考下。
    2018-12-12
  • Qt中鼠标点击的几种状态

    Qt中鼠标点击的几种状态

    在Qt中,鼠标点击按钮通常会触发一系列的事件,包括pressed、released、clicked等,本文主要介绍了Qt中鼠标点击的几种状态,具有一定的参考价值,感兴趣的可以了解一下
    2023-12-12
  • Qt中布局管理的使用小结

    Qt中布局管理的使用小结

    Qt的布局管理系统提供了简单而强大的机制,确保它们有效地使用空间,本文就介绍了Qt中布局管理的使用,具有一定的参考价值,感兴趣的可以了解一下
    2023-09-09
  • Android App仿微信界面切换时Tab图标变色效果的制作方法

    Android App仿微信界面切换时Tab图标变色效果的制作方法

    这篇文章主要介绍了Android App仿微信界面切换时Tab图标变色效果的制作方法,重点讲解了图标的绘制技巧,需要的朋友可以参考下
    2016-04-04
  • c++实现新年烟花效果完整代码

    c++实现新年烟花效果完整代码

    这篇文章主要给大家介绍了关于c++实现新年烟花效果的相关资料,文中给出了详细完整代码,适合初学C语言/C++的小伙伴学习研究,需要的朋友可以参考下
    2023-11-11
  • C++基于reactor的服务器百万并发实现与讲解

    C++基于reactor的服务器百万并发实现与讲解

    这篇文章主要介绍了C++基于reactor的服务器百万并发实现与讲解,本文通过实例图文相结合给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-07-07
  • C++解析obj模型文件方法介绍

    C++解析obj模型文件方法介绍

    由于本人打算使用Assimp来加载模型,这里记录一下tinyobjloader库的使用。之前也研究过fbxsdk,除了骨骼动画暂未读取外,代码自认为还算可靠
    2022-09-09

最新评论