运算符重载
作者:互联网
一.写时拷贝
在发生修改的时候,才会将父进程的内容复制给子进程,没发生修改的不复制,
二.mstring写时拷贝
#pragma once #ifndef MSTRING_H #define MSTRING_H #include<iostream> using namespace std; #include<mutex> class Mstring { private: int _len; char* _str; void init_count() { *((int*)_str) = 1; } int& get_count() { return *((int*)_str); } const int& get_count()const { return *((int*)_str); } void up_count() { get_lock()->lock(); get_count()++; get_lock()->unlock(); } int down_count() { get_lock()->lock(); --get_count(); get_lock()->unlock(); return get_count(); } char* get_str_begin()const { return _str + get_front_len(); } mutex*& get_lock() { return *((mutex**)(_str + 4)); } void init() { init_count(); get_lock() = new mutex(); } int get_front_len()const { return sizeof(int) + sizeof(mutex*); } public: Mstring(int len = 18); Mstring(const Mstring& src); Mstring(const char* str); ~Mstring(); Mstring& operator=(const Mstring& src); Mstring& operator=(const char* str); Mstring operator+(const Mstring& src)const; Mstring operator+(const char* str)const; bool operator==(const Mstring& src)const; bool operator==(const char* str)const; bool operator>(const Mstring& src)const; bool operator>(const char* str)const; ostream& operator<<(ostream& out)const; istream& operator>>(istream& in); char& operator*() { if (1 != get_count()) { char* tmp = new char[_len]; memset(tmp, 0, _len); for (int i = 0; i < size() + 1; i++) { tmp[sizeof(int) + i] = get_str_begin()[i]; } down_count(); _str = tmp; init_count(); } return *_str; } const char& operator*()const { return *_str; } char& operator[](int sit) { if (1 != get_count()) { char* tmp = new char[_len]; memset(tmp, 0, _len); for (int i = 0; i < size() + 1; i++) { tmp[sizeof(int) + i] = get_str_begin()[i]; } down_count(); _str = tmp; init_count(); } return _str[sit]; } const char& operator[](int sit)const { return _str[sit]; } int size()const { return strlen(get_str_begin()); } friend Mstring operator+(const char* str, const Mstring& src); friend bool operator==(const char* str, const Mstring& src); friend bool operator>(const char* str, const Mstring& src); friend ostream& operator<<(ostream& out, const Mstring& src); friend istream& operator>>(istream& in, Mstring& src); }; Mstring operator+(const char* str, const Mstring& src); bool operator==(const char* str, const Mstring& src); bool operator>(const char* str, const Mstring& src); ostream& operator<<(ostream& out, const Mstring& src); istream& operator>>(istream& in, Mstring& src); #endif
mstrig写时拷贝的实现:
#include"mstring.h" Mstring::Mstring(int len) { _len = len; _str = new char[_len]; memset(_str, 0, _len); init(); } Mstring::Mstring(const Mstring& src) { _len = src._len; _str = src._str; up_count(); } Mstring::Mstring(const char* str) { //开辟空间 _len = strlen(str) + 1 + get_front_len(); _str = new char[_len]; memset(_str, 0, _len); //拷贝数据 for (int i = 0; i < strlen(str) + 1; i++) { get_str_begin()[i] = str[i]; } //初始化引用技术 init(); } Mstring::~Mstring() { if (0 == down_count()) { delete get_lock(); delete[]_str; } _len = 0; _str = NULL; } Mstring& Mstring::operator=(const Mstring& src) { if (&src == this) { return *this; } if (0 == down_count()) { delete _str; } _len = src._len; _str = src._str; up_count(); return *this; } Mstring& Mstring::operator=(const char* str) { if (0 == down_count()) { delete get_lock(); delete[]_str; } if (NULL == str) { _len = 14; _str = new char[_len]; memset(_str, 0, _len); init_count(); } _len = strlen(str) + 1 + get_front_len(); _str = new char[_len]; memset(_str, 0, _len); for (int i = 0; i < strlen(str) + 1; i++) { get_str_begin()[i] = str[i]; } init(); return *this; } Mstring Mstring::operator+(const Mstring& src)const { int len = src.size() + size() + 1; char* tmp = new char[len]; memset(tmp, 0, len); int i = 0; for (; i < size(); i++) { tmp[i] = get_str_begin()[i]; } for (int j = 0; j < src.size() + 1; i++, j++) { tmp[i] = src.get_str_begin()[j]; } return tmp; } Mstring Mstring::operator+(const char* str)const { if (NULL == str) { return *this; } int len = size() + strlen(str) + 1; char* tmp = new char[len]; memset(tmp, 0, len); int i = 0; for (; i < size(); i++) { tmp[i] = get_str_begin()[i]; } for (int j = 0; j < strlen(str) + 1; i++, j++) { tmp[i] = str[j]; } return tmp; } bool Mstring::operator==(const Mstring& src)const { return strcmp(get_str_begin(), src.get_str_begin()) == 0; } bool Mstring::operator==(const char* str)const { return strcmp(get_str_begin(), str) == 0; } bool Mstring::operator>(const Mstring& src)const { return strcmp(get_str_begin(), src.get_str_begin()) > 0; } bool Mstring::operator>(const char* str)const { return strcmp(get_str_begin(), str) > 0; } ostream& Mstring::operator<<(ostream& out)const { out << get_str_begin() << endl; return out; } istream& Mstring::operator>>(istream& in) { char str[1024] = { 0 }; in >> str; if (0 == down_count()) { delete _str; } _len = strlen(str) + 1 + sizeof(int); _str = new char[_len]; memset(_str, 0, _len); for (int i = 0; i < strlen(str) + 1; i++) { get_str_begin()[i] = str[i]; } init_count(); return in; } Mstring operator+(const char* str, const Mstring& src) { return src + str; } bool operator==(const char* str, const Mstring& src) { return strcmp(str, src.get_str_begin()) == 0; } bool operator>(const char* str, const Mstring& src) { return strcmp(str, src.get_str_begin()) > 0; } ostream& operator<<(ostream& out, const Mstring& src) { out << "count=" << src.get_count() << endl; out << src.get_str_begin() << endl; return out; } istream& operator>>(istream& in, Mstring& src) { return src.operator>>(in); }
测试:
#include<iostream> #include"mstring.h" using namespace std; int main() { Mstring s1 = "aaa"; Mstring s2 = "bbbb"; Mstring s3; s3 = s1; Mstring* s4 = new Mstring(s3); delete s4; s1[2]; cout << s1 << s2 << s3 << endl; return 0; }
标签:char,const,Mstring,get,len,运算符,str,重载 来源: https://www.cnblogs.com/xpei-1124/p/14861873.html