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


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
struct Aux
{
    int length{ 0 };
    char* buff{ nullptr };
    int count{ 0 };
};

class MyStr {
public:
    Aux* pAux{ nullptr };
    MyStr() : pAux(nullptr)
    {
        cout << "  MyStr default constr.\n";
    }

    MyStr(const char* init_val) : pAux(nullptr) //MyStr ms = "Hi";
    {
        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 = 1;
        }
        cout << "  MyStr( char * ) constr.\n";
    }

    MyStr(const MyStr& other) : pAux(nullptr)   //MyStr ms2 = ms;  //length(other.length)
    {
        cout << "  MyStr copy const.\n";
        if (other.pAux != nullptr) { //(length > 0) {
            pAux = other.pAux;
            pAux->count++;
            /*buff = new char[length + 1];
            strcpy(buff, other.buff);*/
        }       
    }

    ~MyStr()
    {
        cout << "  MyStr destr.\n";
        if (pAux == nullptr)
            return;
        else
        {
            pAux->count--;
            if (pAux->count == 0)
            {
                delete[] pAux->buff;
                delete pAux;
                pAux = nullptr;
            }
        }
        //delete[] buff;
    }

    int size() { 
        if (pAux == nullptr)
            return 0;
        else
            return pAux->length;
    }

    MyStr& operator= (const MyStr& other)
    {
        cout << "  MyStr::operator=\n";
        if (this != &other && pAux != other.pAux) {
            if (pAux != nullptr && other.pAux == nullptr)
            {
                pAux->count--;
                if (pAux->count == 0)
                {
                    delete[] pAux->buff;
                    delete pAux;
                    pAux = nullptr;
                }
            }

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


    friend MyStr operator+(const MyStr& s1, const MyStr& s2);
    friend ostream& operator<< (ostream&, const MyStr&);

    char& operator[] (int index)
    {
        if (index < 0 || index > length) {
            cerr << "Invalid index in MyStr::operator[]. Aborting...\n";
            exit(-1);
        }
        return buff[index];
    }
};
MyStr operator+(const MyStr& s1, const MyStr& s2)
{
    MyStr res;
    if (s1.length == 0 && s2.length == 0)
        return res;

    res.length = s1.length + s2.length;
    res.buff = new char[res.length + 1];
    if (s1.length != 0)
        strcpy(res.buff, s1.buff);
    if (s2.length != 0)
        strcat(res.buff, s2.buff);

    return res;
}
ostream& operator<< (ostream& os, const MyStr& s) {
    for (int i = 0; i < s.length; i++)  os.put(s.buff[i]);
    return os;
}


int main() {
    MyStr a = "Hello ";
    MyStr b = "World!";
    MyStr c = a;
    MyStr c2;
    c2 = a;
    c = a + b;
    cout << c << endl;

    return 0;
}