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

相关文章

Idea实现接口的方法上无法添加@Override注解的解决方案

《Idea实现接口的方法上无法添加@Override注解的解决方案》文章介绍了在IDEA中实现接口方法时无法添加@Override注解的问题及其解决方法,主要步骤包括更改项目结构中的Languagel... 目录Idea实现接China编程口的方法上无法添加@javascriptOverride注解错误原因解决方

轻松上手MYSQL之JSON函数实现高效数据查询与操作

《轻松上手MYSQL之JSON函数实现高效数据查询与操作》:本文主要介绍轻松上手MYSQL之JSON函数实现高效数据查询与操作的相关资料,MySQL提供了多个JSON函数,用于处理和查询JSON数... 目录一、jsON_EXTRACT 提取指定数据二、JSON_UNQUOTE 取消双引号三、JSON_KE

MySql死锁怎么排查的方法实现

《MySql死锁怎么排查的方法实现》本文主要介绍了MySql死锁怎么排查的方法实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录前言一、死锁排查方法1. 查看死锁日志方法 1:启用死锁日志输出方法 2:检查 mysql 错误

CSS3中使用flex和grid实现等高元素布局的示例代码

《CSS3中使用flex和grid实现等高元素布局的示例代码》:本文主要介绍了使用CSS3中的Flexbox和Grid布局实现等高元素布局的方法,通过简单的两列实现、每行放置3列以及全部代码的展示,展示了这两种布局方式的实现细节和效果,详细内容请阅读本文,希望能对你有所帮助... 过往的实现方法是使用浮动加

Go Mongox轻松实现MongoDB的时间字段自动填充

《GoMongox轻松实现MongoDB的时间字段自动填充》这篇文章主要为大家详细介绍了Go语言如何使用mongox库,在插入和更新数据时自动填充时间字段,从而提升开发效率并减少重复代码,需要的可以... 目录前言时间字段填充规则Mongox 的安装使用 Mongox 进行插入操作使用 Mongox 进行更

MySQL修改密码的四种实现方式

《MySQL修改密码的四种实现方式》文章主要介绍了如何使用命令行工具修改MySQL密码,包括使用`setpassword`命令和`mysqladmin`命令,此外,还详细描述了忘记密码时的处理方法,包... 目录mysql修改密码四种方式一、set password命令二、使用mysqladmin三、修改u

c++中std::placeholders的使用方法

《c++中std::placeholders的使用方法》std::placeholders是C++标准库中的一个工具,用于在函数对象绑定时创建占位符,本文就来详细的介绍一下,具有一定的参考价值,感兴... 目录1. 基本概念2. 使用场景3. 示例示例 1:部分参数绑定示例 2:参数重排序4. 注意事项5.

使用C++将处理后的信号保存为PNG和TIFF格式

《使用C++将处理后的信号保存为PNG和TIFF格式》在信号处理领域,我们常常需要将处理结果以图像的形式保存下来,方便后续分析和展示,C++提供了多种库来处理图像数据,本文将介绍如何使用stb_ima... 目录1. PNG格式保存使用stb_imagephp_write库1.1 安装和包含库1.2 代码解

Java实现状态模式的示例代码

《Java实现状态模式的示例代码》状态模式是一种行为型设计模式,允许对象根据其内部状态改变行为,本文主要介绍了Java实现状态模式的示例代码,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来... 目录一、简介1、定义2、状态模式的结构二、Java实现案例1、电灯开关状态案例2、番茄工作法状态案例

一文教你使用Python实现本地分页

《一文教你使用Python实现本地分页》这篇文章主要为大家详细介绍了Python如何实现本地分页的算法,主要针对二级数据结构,文中的示例代码简洁易懂,有需要的小伙伴可以了解下... 在项目开发的过程中,遇到分页的第一页就展示大量的数据,导致前端列表加载展示的速度慢,所以需要在本地加入分页处理,把所有数据先放