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


#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) { // Concatenation operator that accepts two instances of MyStr class
//    MyStr res; // Create the result instance of MyStr class
//    res.length = s1.length + s2.length; // Assign s1 and s2 instances length to result instance length
//    res.buff = new char[res.length + 1]; // Create a new buffer on result instance
//    if (s1.buff) strcpy(res.buff, s1.buff); else res.buff[0] = '\0'; // Copy s1 buffer to result buffer if not empty, else make result buffer nullptr
//    if (s2.buff) strcat(res.buff, s2.buff); // Append s2 buffer to result buffer if not empty
//    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 << a << endl;

    return 0;

}