C++类中的拷贝构造、运算符重载实现

2024-08-22 10:18

本文主要是介绍C++类中的拷贝构造、运算符重载实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

最近学习了C++类的拷贝构造函数与运算符重载,据此模仿写了个string类的实现,完成了字符串的部分功能,实现了拷贝构造函数和运算符重载的函数。本文仅作为练习的记录,不讲理论。直接上代码

string.h

//
// Created by xiangqian on 18-3-3.
//#ifndef STRING_STRING_H
#define STRING_STRING_H#include <stdio.h>namespace xq {class String {public:String(const char * = NULL);~String();String(const String &);// String a; a = b;String& operator=(const String &);// String a; a = "hello";String& operator=(const char *);String& operator+=(const String &);String operator+(const String &) const ;String& operator+=(const char *);String operator+(const char *) const;inline const char *data() const {return m_data;}private:char *m_data;};}#endif //STRING_STRING_H

string.cpp

//
// Created by xiangqian on 18-3-3.
//
#include "String.h"
#include <string.h>
#include <iostream>using namespace xq;
using namespace std;String::String(const char *str) {cout << "constructor" << endl;if (NULL == str) {m_data = new char[1];*m_data = '\0';} else {int len = strlen(str);m_data = new char[len + 1];strcpy(m_data, str);}
}String::~String() {delete[] m_data;
}String::String(const String &other) {cout << "copy constructor" << endl;int len = strlen(other.m_data);m_data = new char[len + 1];strcpy(m_data, other.m_data);
}String &String::operator=(const String &other) {cout << "operator=(const String &)" << endl;if (this == &other)return *this;delete[] m_data;int len = strlen(other.m_data);m_data = new char[len + 1];strcpy(m_data, other.m_data);return *this;
}String &String::operator=(const char *str) {cout << "operator=(const char *)" << endl;delete[] m_data;if (NULL == str) {m_data = new char[1];*m_data = '\0';} else {int len = strlen(str);m_data = new char[len + 1];strcpy(m_data, str);}return *this;
}String &String::operator+=(const String &other) {cout << "operator+=(const String &)" << endl;int len = strlen(m_data) + strlen(other.m_data);char *temp = m_data;m_data = new char[len + 1];strcpy(m_data, temp);strcat(m_data, other.m_data);delete[] temp;return *this;
}String String::operator+(const String &other) const {cout << "operator+(const String &)" << endl;String result;result = *this;result += other;return result;
}String& String::operator+=(const char * str) {cout << "operator+=(const char *)" << endl;strcat(m_data,str);return *this;
}String String::operator+(const char * str) const {cout << "operator+(const char *)" << endl;strcat(m_data,str);return *this;
}

测试代码 main.cpp

#include <iostream>
#include "String.h"using namespace std;
using namespace xq;int main() {String a("Hello");String b = a;String c;String d;c = a;c = "World!";cout << d.data() << endl;d += a;cout << d.data() << endl;d += "World!";cout << d.data() << endl;cout << "=========================" << endl;cout << (a+c).data() << endl;cout << (a+"World!").data() << endl;return 0;
}

最终输出为:

constructor
copy constructor
constructor
constructor
operator=(const String &)
operator=(const char *)


operator+=(const String &)
Hello
operator+=(const char *)
HelloWorld!
=========================
operator+(const String &)
constructor
operator=(const String &)
operator+=(const String &)
HelloWorld!
operator+(const char *)
copy constructor
HelloWorld!



这篇关于C++类中的拷贝构造、运算符重载实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1095988

相关文章

python生成随机唯一id的几种实现方法

《python生成随机唯一id的几种实现方法》在Python中生成随机唯一ID有多种方法,根据不同的需求场景可以选择最适合的方案,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习... 目录方法 1:使用 UUID 模块(推荐)方法 2:使用 Secrets 模块(安全敏感场景)方法

C++中全局变量和局部变量的区别

《C++中全局变量和局部变量的区别》本文主要介绍了C++中全局变量和局部变量的区别,全局变量和局部变量在作用域和生命周期上有显著的区别,下面就来介绍一下,感兴趣的可以了解一下... 目录一、全局变量定义生命周期存储位置代码示例输出二、局部变量定义生命周期存储位置代码示例输出三、全局变量和局部变量的区别作用域

C++中assign函数的使用

《C++中assign函数的使用》在C++标准模板库中,std::list等容器都提供了assign成员函数,它比操作符更灵活,支持多种初始化方式,下面就来介绍一下assign的用法,具有一定的参考价... 目录​1.assign的基本功能​​语法​2. 具体用法示例​​​(1) 填充n个相同值​​(2)

Spring StateMachine实现状态机使用示例详解

《SpringStateMachine实现状态机使用示例详解》本文介绍SpringStateMachine实现状态机的步骤,包括依赖导入、枚举定义、状态转移规则配置、上下文管理及服务调用示例,重点解... 目录什么是状态机使用示例什么是状态机状态机是计算机科学中的​​核心建模工具​​,用于描述对象在其生命

Spring Boot 结合 WxJava 实现文章上传微信公众号草稿箱与群发

《SpringBoot结合WxJava实现文章上传微信公众号草稿箱与群发》本文将详细介绍如何使用SpringBoot框架结合WxJava开发工具包,实现文章上传到微信公众号草稿箱以及群发功能,... 目录一、项目环境准备1.1 开发环境1.2 微信公众号准备二、Spring Boot 项目搭建2.1 创建

IntelliJ IDEA2025创建SpringBoot项目的实现步骤

《IntelliJIDEA2025创建SpringBoot项目的实现步骤》本文主要介绍了IntelliJIDEA2025创建SpringBoot项目的实现步骤,文中通过示例代码介绍的非常详细,对大家... 目录一、创建 Spring Boot 项目1. 新建项目2. 基础配置3. 选择依赖4. 生成项目5.

Linux下删除乱码文件和目录的实现方式

《Linux下删除乱码文件和目录的实现方式》:本文主要介绍Linux下删除乱码文件和目录的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux下删除乱码文件和目录方法1方法2总结Linux下删除乱码文件和目录方法1使用ls -i命令找到文件或目录

SpringBoot+EasyExcel实现自定义复杂样式导入导出

《SpringBoot+EasyExcel实现自定义复杂样式导入导出》这篇文章主要为大家详细介绍了SpringBoot如何结果EasyExcel实现自定义复杂样式导入导出功能,文中的示例代码讲解详细,... 目录安装处理自定义导出复杂场景1、列不固定,动态列2、动态下拉3、自定义锁定行/列,添加密码4、合并

mybatis执行insert返回id实现详解

《mybatis执行insert返回id实现详解》MyBatis插入操作默认返回受影响行数,需通过useGeneratedKeys+keyProperty或selectKey获取主键ID,确保主键为自... 目录 两种方式获取自增 ID:1. ​​useGeneratedKeys+keyProperty(推

Spring Boot集成Druid实现数据源管理与监控的详细步骤

《SpringBoot集成Druid实现数据源管理与监控的详细步骤》本文介绍如何在SpringBoot项目中集成Druid数据库连接池,包括环境搭建、Maven依赖配置、SpringBoot配置文件... 目录1. 引言1.1 环境准备1.2 Druid介绍2. 配置Druid连接池3. 查看Druid监控