C++基础知识 - 赋值构造函数

这篇博客探讨了C++中的赋值构造函数。如果没有显式定义,编译器会提供一个默认的赋值构造函数,进行浅拷贝操作。文章通过示例代码Human.h, Human.cpp和main.cpp来阐述这一概念。" 112410401,10295786,IDEA REST Client:超越Postman的接口调试利器,"['开发工具', '接口测试', 'RESTful API', 'HTTP客户端']
部署运行你感兴趣的模型镜像

赋值构造函数

  • 如果没有定义赋值构造函数,编译器会自动定义“合成的赋值构造函数”,
    与其他合成的构造函数,是“浅拷贝”(又称为“位拷贝”)。
定义: 
Human& operator=(const Human& other);

实现: 
Human& Human::operator=(const Human& other){
	//当other = other时;
	if (this == &other) return *this;	

	//假如 f1 = f2;
	//自动调用, f1.operator=(f2)
	this->name = other.name;
	this->age = other.age;
	this->sex = other.sex;

	strcpy_s(this->addr,ADDR_LEN, other.addr);
	//返回对象的引用,是为了做链式处理: f1 = f2 = f3;
	return *this;
}

调用: 
对象赋值时自动调用
	//调用赋值构造函数
	lisi = zhangsan;

 
Human.h

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

class Human {
public:		
	Human();
	~Human();
	Human(string name, int age, string sex);

	//定义了一个赋值构造函数
	Human& operator=(const Human& other);

	string getName() const;
	string getSex() const;
	int getAge() const;
	const char* getAddr()const;
	void setAddr(char* addr);
	void description() const;	
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(string name, int age, string sex) {
	this->age = age;
	this->name = name;
	this->sex = sex;
	const char* addr_s = "China";
	addr = new char[ADDR_LEN];
	strcpy_s(addr, ADDR_LEN, addr_s);
}

Human& Human::operator=(const Human& other){
	//当other = other时;
	if (this == &other) return *this;	

	//假如 f1 = f2;
	//自动调用, f1.operator=(f2)
	this->name = other.name;
	this->age = other.age;
	this->sex = other.sex;

	strcpy_s(this->addr,ADDR_LEN, other.addr);
	//返回对象的引用,是为了做链式处理: f1 = f2 = f3;
	return *this;
}

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

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

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

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

void Human::setAddr(char* addr){
	if (!addr) 	return;
	strcpy_s(this->addr, ADDR_LEN, addr);
}

 
main.cpp

#include "Human.h"
using namespace std;

void showMsg(const Human &man1, const Human& man2) {
	cout << "张三地址: " << man1.getAddr() << endl;
	cout << "李四地址: " << man2.getAddr() << endl;
}

int main(void) {
	Human zhangsan("张三", 18, "男");	
	Human lisi;

	//调用赋值构造函数
	lisi = zhangsan;

	cout << "张三修改地址前" << endl;
	showMsg(zhangsan, lisi);
	
	zhangsan.setAddr((char*)"新加坡");
	cout << "张三修改地址后" << endl;
	showMsg(zhangsan, lisi);

	system("pause");
	return 0;
}

您可能感兴趣的与本文相关的镜像

ACE-Step

ACE-Step

音乐合成
ACE-Step

ACE-Step是由中国团队阶跃星辰(StepFun)与ACE Studio联手打造的开源音乐生成模型。 它拥有3.5B参数量,支持快速高质量生成、强可控性和易于拓展的特点。 最厉害的是,它可以生成多种语言的歌曲,包括但不限于中文、英文、日文等19种语言

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值