欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++中拷貝構(gòu)造函數(shù)的使用

 更新時(shí)間:2022年02月14日 14:40:49   作者:駱駝胡楊  
大家好,本篇文章主要講的是C++中拷貝構(gòu)造函數(shù)的使用,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下

拷貝構(gòu)造函數(shù)

拷貝構(gòu)造函數(shù),它只有一個(gè)參數(shù),參數(shù)類型是本類的引用。
復(fù)制構(gòu)造函數(shù)的參數(shù)可以是 const 引用,也可以是非 const 引用。 一般使用前者,這樣既能以常量對象(初始化后值不能改變的對象)作為參數(shù),也能以非常量對象作為參數(shù)去初始化其他對象。一個(gè)類中寫兩個(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對象的數(shù)據(jù)拷貝到另一個(gè)對象的私有數(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ù)拷貝到另一個(gè)對象的私有數(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)程序員沒有定義拷貝構(gòu)造函數(shù)時(shí), 編譯器會自動(dòng)生成合成的拷貝構(gòu)造函數(shù)

說明:
合成的拷貝構(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 = "無名";
	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;

	//使用深拷貝, 單獨(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*)"美國");

	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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • c++下迭代器總結(jié)

    c++下迭代器總結(jié)

    大家好,本篇文章主要講的是c++下迭代器總結(jié),感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽
    2021-12-12
  • C語言的變量與常量 字符字符串與轉(zhuǎn)義字符詳解

    C語言的變量與常量 字符字符串與轉(zhuǎn)義字符詳解

    這篇文章主要介紹了詳解C語言的變量與常量 字符字符串與轉(zhuǎn)義字符,包括其之間的區(qū)別是C語言入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下
    2021-10-10
  • 深入理解C預(yù)處理器

    深入理解C預(yù)處理器

    下面小編就為大家?guī)硪黄钊肜斫釩預(yù)處理器。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-08-08
  • C++實(shí)現(xiàn)遞歸函數(shù)的方法

    C++實(shí)現(xiàn)遞歸函數(shù)的方法

    在本篇內(nèi)容里小編給大家分享了關(guān)于C++實(shí)現(xiàn)遞歸函數(shù)的教學(xué)步驟,需要的朋友跟著參考下。
    2018-12-12
  • Qt中鼠標(biāo)點(diǎn)擊的幾種狀態(tài)

    Qt中鼠標(biāo)點(diǎn)擊的幾種狀態(tài)

    在Qt中,鼠標(biāo)點(diǎn)擊按鈕通常會觸發(fā)一系列的事件,包括pressed、released、clicked等,本文主要介紹了Qt中鼠標(biāo)點(diǎn)擊的幾種狀態(tài),具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-12-12
  • Qt中布局管理的使用小結(jié)

    Qt中布局管理的使用小結(jié)

    Qt的布局管理系統(tǒng)提供了簡單而強(qiáng)大的機(jī)制,確保它們有效地使用空間,本文就介紹了Qt中布局管理的使用,具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-09-09
  • Android App仿微信界面切換時(shí)Tab圖標(biāo)變色效果的制作方法

    Android App仿微信界面切換時(shí)Tab圖標(biāo)變色效果的制作方法

    這篇文章主要介紹了Android App仿微信界面切換時(shí)Tab圖標(biāo)變色效果的制作方法,重點(diǎn)講解了圖標(biāo)的繪制技巧,需要的朋友可以參考下
    2016-04-04
  • c++實(shí)現(xiàn)新年煙花效果完整代碼

    c++實(shí)現(xiàn)新年煙花效果完整代碼

    這篇文章主要給大家介紹了關(guān)于c++實(shí)現(xiàn)新年煙花效果的相關(guān)資料,文中給出了詳細(xì)完整代碼,適合初學(xué)C語言/C++的小伙伴學(xué)習(xí)研究,需要的朋友可以參考下
    2023-11-11
  • C++基于reactor的服務(wù)器百萬并發(fā)實(shí)現(xiàn)與講解

    C++基于reactor的服務(wù)器百萬并發(fā)實(shí)現(xiàn)與講解

    這篇文章主要介紹了C++基于reactor的服務(wù)器百萬并發(fā)實(shí)現(xiàn)與講解,本文通過實(shí)例圖文相結(jié)合給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-07-07
  • C++解析obj模型文件方法介紹

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

    由于本人打算使用Assimp來加載模型,這里記錄一下tinyobjloader庫的使用。之前也研究過fbxsdk,除了骨骼動(dòng)畫暫未讀取外,代碼自認(rèn)為還算可靠
    2022-09-09

最新評論