程序笔记   发布时间:2022-07-20  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了运算符重载 mstring写时拷贝大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

一.写时拷贝

在发生修改的时候,才会将父进程的内容复制给子进程,没发生修改的不复制,

二.mString写时拷贝

运算符重载  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);
    ~@H_81_17@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
@H_244_2@mstrig写时拷贝的实现:

#include"@H_498_501@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::~@H_81_17@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"@H_498_501@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;
}

 

大佬总结

以上是大佬教程为你收集整理的运算符重载 mstring写时拷贝全部内容,希望文章能够帮你解决运算符重载 mstring写时拷贝所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。