本文主要是介绍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++类中的拷贝构造、运算符重载实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!