C++中拷貝構(gòu)造函數(shù)的使用
拷貝構(gòu)造函數(shù)
拷貝構(gòu)造函數(shù),它只有一個(gè)參數(shù),參數(shù)類(lèi)型是本類(lèi)的引用。
復(fù)制構(gòu)造函數(shù)的參數(shù)可以是 const 引用,也可以是非 const 引用。 一般使用前者,這樣既能以常量對(duì)象(初始化后值不能改變的對(duì)象)作為參數(shù),也能以非常量對(duì)象作為參數(shù)去初始化其他對(duì)象。一個(gè)類(lèi)中寫(xiě)兩個(gè)復(fù)制構(gòu)造函數(shù),一個(gè)的參數(shù)是 const 引用,另一個(gè)的參數(shù)是非 const 引用,也是可以的。
1. 手動(dòng)定義的拷貝構(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);
//手動(dòng)定義了一個(gè)拷貝構(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對(duì)象的數(shù)據(jù)拷貝到另一個(gè)對(duì)象的私有數(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對(duì)象的數(shù)據(jù)拷貝到另一個(gè)對(duì)象的私有數(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ù)
當(dāng)程序員沒(méi)有定義拷貝構(gòu)造函數(shù)時(shí), 編譯器會(huì)自動(dòng)生成合成的拷貝構(gòu)造函數(shù)
說(shuō)明:
合成的拷貝構(gòu)造函數(shù)的缺點(diǎn): 使用“淺拷貝”

解決方案:在自定義的拷貝構(gòu)造函數(shù)中,使用‘深拷貝
Human.h
#pragma once
#include <string>
#include <iostream>
#include <Windows.h>
using namespace std;
class Human {
public:
Human();
//定義了一個(gè)拷貝構(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 = "無(wú)名";
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對(duì)象的數(shù)據(jù)拷貝到私有數(shù)據(jù)
this->name = other.name;
this->sex = other.sex;
this->age = other.age;
//使用深拷貝, 單獨(dú)分配一個(gè)內(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; //自動(dòng)調(diào)用拷貝構(gòu)造函數(shù)
//賦值的時(shí)候調(diào)用的是賦值構(gòu)造函數(shù)
//lisi = zhangsan;
cout <<"李四地址: " << lisi.getAddr() << endl;
cout <<"張三地址: " << zhangsan.getAddr() << endl;
cout << "張三修改地址" << endl;
zhangsan.setAddr((char*)"美國(guó)");
cout << "李四地址: " << lisi.getAddr() << endl;
cout << "張三地址: " << zhangsan.getAddr() << endl;
system("pause");
return 0;
}
總結(jié)
到此這篇關(guān)于C++中拷貝構(gòu)造函數(shù)的使用的文章就介紹到這了,更多相關(guān)C++拷貝構(gòu)造函數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Qt GUI圖形圖像開(kāi)發(fā)之QT表格控件QTableView,QTableWidget復(fù)雜表頭(多行表頭) 及凍結(jié)、固定特
這篇文章主要介紹了Qt GUI圖形圖像開(kāi)發(fā)之QT表格控件QTableView,QTableWidget復(fù)雜表頭(多行表頭) 及凍結(jié)、固定特定的行的詳細(xì)方法與實(shí)例,需要的朋友可以參考下2020-03-03
C++實(shí)現(xiàn)寢室衛(wèi)生管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)寢室衛(wèi)生管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
C語(yǔ)言數(shù)據(jù)結(jié)構(gòu)不掛科指南之棧&隊(duì)列&數(shù)組詳解
自考重點(diǎn)、期末考試必過(guò)指南,這篇文章讓你理解什么是棧、什么是隊(duì)列、什么是數(shù)組。文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-09-09
C語(yǔ)言實(shí)現(xiàn)手寫(xiě)Map(全功能)的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何利用C語(yǔ)言實(shí)現(xiàn)手寫(xiě)Map(全功能),文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C語(yǔ)言有一定幫助,需要的可以參考一下2022-08-08
Windows系統(tǒng)下使用C語(yǔ)言編寫(xiě)單線程的文件備份程序
這篇文章主要介紹了Windows系統(tǒng)下使用C語(yǔ)言編寫(xiě)單線程的文件備份程序,文中給出了實(shí)現(xiàn)的幾個(gè)關(guān)鍵代碼片段,剩下的只要套上main和線程調(diào)用的相關(guān)函數(shù)即可,非常詳細(xì),需要的朋友可以參考下2016-02-02
分享一下8年C++面向?qū)ο笤O(shè)計(jì)的經(jīng)驗(yàn)體會(huì)
關(guān)于C++程序設(shè)計(jì)的書(shū)藉非常多,本章不講C++的語(yǔ)法,只講一些小小的編程道理。如果我能早幾年明白這些小道理,就可以大大改善數(shù)十萬(wàn)行程序的質(zhì)量了2017-07-07

