網(wǎng)站首頁 編程語言 正文
拷貝構(gòu)造函數(shù)
拷貝構(gòu)造函數(shù),它只有一個參數(shù),參數(shù)類型是本類的引用。
復(fù)制構(gòu)造函數(shù)的參數(shù)可以是 const 引用,也可以是非 const 引用。 一般使用前者,這樣既能以常量對象(初始化后值不能改變的對象)作為參數(shù),也能以非常量對象作為參數(shù)去初始化其他對象。一個類中寫兩個復(fù)制構(gòu)造函數(shù),一個的參數(shù)是 const 引用,另一個的參數(shù)是非 const 引用,也是可以的。
1. 手動定義的拷貝構(gòu)造函數(shù)
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); //手動定義了一個拷貝構(gòu)造函數(shù) 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; } //拷貝構(gòu)造函數(shù) Human::Human(const Human& other){ //把other對象的數(shù)據(jù)拷貝到另一個對象的私有數(shù)據(jù) 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; } //拷貝構(gòu)造函數(shù) Human::Human(const Human& other){ //把other對象的數(shù)據(jù)拷貝到另一個對象的私有數(shù)據(jù) 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. 合成的拷貝構(gòu)造函數(shù)
當程序員沒有定義拷貝構(gòu)造函數(shù)時, 編譯器會自動生成合成的拷貝構(gòu)造函數(shù)
說明:
合成的拷貝構(gòu)造函數(shù)的缺點: 使用“淺拷貝”
解決方案:在自定義的拷貝構(gòu)造函數(shù)中,使用‘深拷貝
Human.h
#pragma once #include <string> #include <iostream> #include <Windows.h> using namespace std; class Human { public: Human(); //定義了一個拷貝構(gòu)造函數(shù) Human(const Human & man); string getName() const; string getSex() const; int getAge() const; const char* getAddr(); void setAddr(char* addr); //設(shè)置地址 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); } //拷貝構(gòu)造函數(shù) Human::Human(const Human& other){ cout << "調(diào)用拷貝構(gòu)造函數(shù)" << endl; //把other對象的數(shù)據(jù)拷貝到私有數(shù)據(jù) this->name = other.name; this->sex = other.sex; this->age = other.age; //使用深拷貝, 單獨分配一個內(nèi)存 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; //初始化調(diào)用拷貝構(gòu)造函數(shù) Human lisi = zhangsan; //自動調(diào)用拷貝構(gòu)造函數(shù) //賦值的時候調(diào)用的是賦值構(gòu)造函數(shù) //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; }
總結(jié)
原文鏈接:https://blog.csdn.net/qq_34606496/article/details/122914060
相關(guān)推薦
- 2022-04-20 C語言函數(shù)棧幀的創(chuàng)建和銷毀詳解_C 語言
- 2024-01-14 在springboot中給mybatis加攔截器
- 2022-06-10 redis?解決庫存并發(fā)問題實現(xiàn)數(shù)量控制_Redis
- 2022-12-22 利用C++求解八數(shù)碼問題實例代碼_C 語言
- 2023-01-08 Android?Application的使用全面解析_Android
- 2022-09-08 pytest實現(xiàn)多進程與多線程運行超好用的插件_python
- 2022-02-10 easy LESS只針對特定工程啟用
- 2022-07-30 沒有匹配的倉庫可以修改:PowerTools
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學(xué)習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支