Загрузка данных


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstring>
using namespace std;
struct Aux
{
    int length{ 0 };
    char* buff{ nullptr };
    int count = 0;
};
class MyStr {
    //int length{ 0 }; char* buff{ nullptr };
public:
    Aux* pAux{ nullptr };
    MyStr() : pAux(nullptr) {
        cout << "  MyStr default constr.\n";
    }
    MyStr(const char* init_val) {
        if (init_val != nullptr && *init_val != '\0') {
            pAux = new Aux();
            pAux->length = strlen(init_val);
            pAux->buff = new char[pAux->length + 1];
            strcpy(pAux->buff, init_val);
            pAux->count++;
        }
        cout << "  MyStr( char * ) constr.\n";
    }
    MyStr(const MyStr& other) : pAux(nullptr) {
        cout << "MyStr::CopyCtor()" << endl;
        if (other.pAux == nullptr)
            return;
        else
        {
            pAux = other.pAux;
            pAux->count++;
        }
        /*if (pAux == nullptr && other.pAux == nullptr)
            return;
        if (pAux == nullptr && other.pAux != nullptr)
        {
            pAux = other.pAux;
            pAux->count++;
        }
        if (pAux != nullptr && other.pAux == nullptr)*/

        /*if (other.length != 0) {
            this->length = other.length;
            buff = new char[length + 1];
            strcpy(buff, other.buff);
        }*/

    }
    ~MyStr() {
        cout << "  MyStr destr.\n";
        /*delete[] buff;*/
        pAux->count--;
        if (pAux->count == 0)
        {
            delete[] pAux->buff;
            pAux->buff = nullptr;
            pAux->length = 0;
        }
        pAux = nullptr;
    }
    int size() { return pAux->length; }
    MyStr& operator= (const MyStr& rhs) { // const MyStr& rhs = m_DerivedFromMyStr
        if (this == &rhs)
            return *this;
        if (pAux == nullptr && rhs.pAux == nullptr) //null = null
            return *this;
        if (pAux == nullptr) //null = "hi"
        {
            pAux = rhs.pAux;
            pAux->count++;
            return *this;
        }
        if (pAux != nullptr && rhs.pAux == nullptr) //"hi" = null
        {
            pAux->count--;
            if (pAux->count == 0)
            {
                delete[] pAux->buff;
                pAux->buff = nullptr;
                pAux->length = 0;
            }
            pAux = nullptr;
            return *this;
        }
        if (pAux != nullptr && rhs.pAux != nullptr)
        {
            pAux->count--;
            if (pAux->count == 0)
            {
                delete[] pAux->buff;
                pAux->buff = nullptr;
                pAux->length = 0;
            }

            pAux = rhs.pAux;
            pAux->count++;
            return *this;
        }

        return *this;
    }
    friend MyStr operator+(const MyStr& s1, const MyStr& s2);
    friend ostream& operator<< (ostream&, const MyStr&);
    /*char& operator[] (int index) {
        return this->buff[index];
    }*/
};
MyStr operator+(const MyStr& s1, const MyStr& s2) {
    MyStr res;

    if (s1.pAux == nullptr && s2.pAux == nullptr)
        return res;

    res.pAux = new Aux();
    res.pAux->count = 1;

    int len1 = 0;
    if (s1.pAux != nullptr)
        len1 = s1.pAux->length;

    int len2 = 0;
    if (s2.pAux != nullptr)
        len2 = s2.pAux->length;
    res.pAux->length = len1 + len2;

    res.pAux->buff = new char[res.pAux->length + 1];

    if (s1.pAux != nullptr && s1.pAux->buff != nullptr)
        strcpy(res.pAux->buff, s1.pAux->buff);
    else
        res.pAux->buff[0] = '\0';

    if (s2.pAux != nullptr && s2.pAux->buff != nullptr)
        strcat(res.pAux->buff, s2.pAux->buff);

    return res;
}
ostream& operator<< (ostream& os, const MyStr& s) {
    if (s.pAux != nullptr)
        return os << s.pAux->buff;
}
MyStr f(MyStr s1) { return s1; }
int main() {
    MyStr a = "Hello, ";
    MyStr b = "World!";
    f(a);

    MyStr c;
    c = a + b;
    cout << c << endl;

    return 0;

}