網站首頁 編程語言 正文
拷貝構造函數
拷貝構造函數,它只有一個參數,參數類型是本類的引用。
復制構造函數的參數可以是 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; }
總結
原文鏈接:https://blog.csdn.net/qq_34606496/article/details/122914060
相關推薦
- 2024-02-16 springmvc中的數據提交方式
- 2022-03-26 C++實現簡單猜數字小游戲_C 語言
- 2022-08-04 Go?slice切片使用示例詳解_Golang
- 2022-07-24 CSP?communicating?sequential?processes并發模型_Golang
- 2021-12-08 Linux之操作文件的系統調用_Linux
- 2023-10-11 MyBatis-plus wrapper.and用法 | apply
- 2022-05-23 Nginx中location匹配以及rewrite重寫跳轉詳解_nginx
- 2022-11-19 Golang?cron?定時器和定時任務的使用場景_Golang
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支