Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string.h>
- using namespace std;
- class MyString
- {
- private:
- char *str;
- int *ref;
- public:
- MyString()
- {
- cout << "empty" << endl;
- ref = nullptr;
- cout << "empty" << endl;
- }
- MyString(const char *init_val)
- {
- cout << "char*" << endl;
- // strcpy(str, init_val);
- ref = new int;
- str = new char[strlen(init_val)];
- // for (size_t i = 0; i < strlen(init_val); i++)
- // {
- // str[i] = init_val[i];
- // }
- strcpy(str, init_val);
- *ref = 1;
- cout << *ref;
- cout << "char*" << endl;
- }
- MyString(MyString &&rhs)
- {
- cout << "rhs&&" << endl;
- str = rhs.str;
- rhs.str = nullptr;
- ref = rhs.ref;
- rhs.ref = nullptr;
- cout << *ref;
- cout << "rhs&&" << endl;
- }
- MyString(const MyString &mystr)
- {
- cout << "rhs&" << endl;
- str = mystr.str;
- ref = mystr.ref;
- (*ref)++;
- cout << *ref;
- cout << "rhs&" << endl;
- }
- MyString &operator=(const MyString &rhs)
- {
- cout << "1" << endl;
- if (ref == nullptr)
- cout << "NULLLLL" << endl;
- else
- cout << "2" << endl;
- str = rhs.str;
- cout << "3" << endl;
- ref = rhs.ref;
- cout << "4" << endl;
- (*ref)++;
- cout << "5" << endl;
- }
- MyString &operator+=(const MyString &rhs)
- {
- if (*ref == 1)
- {
- strcat(str, rhs.str);
- return *this;
- }
- else
- {
- char *temp = str;
- str = new char[strlen(temp) + strlen(rhs.str)];
- strcpy(str, temp);
- strcat(str, rhs.str);
- (*ref)--;
- delete[] temp;
- return *(new MyString(str));
- }
- }
- MyString &operator+=(const char *rhs)
- {
- if (*ref == 1)
- {
- strcat(str, rhs);
- return *this;
- }
- else
- {
- char *temp = str;
- str = new char[strlen(temp) + strlen(rhs)];
- strcpy(str, temp);
- strcat(str, rhs);
- (*ref)--;
- delete[] temp;
- return *(new MyString(str));
- }
- }
- MyString &operator<<(const char *rhs)
- {
- }
- MyString &operator>>(const char *rhs)
- {
- }
- char operator[](const int &i) const
- {
- return str[i];
- }
- int length() const
- {
- return strlen(str);
- }
- char *GetString() const
- {
- return str;
- }
- int GetRef()
- {
- return *ref;
- }
- ~MyString()
- {
- cout << "Destructor" << endl;
- if (*ref == 1)
- {
- delete[] str;
- delete ref;
- }
- else
- {
- (*ref)--;
- }
- }
- void operator++(int i)
- {
- str[0] = 'b';
- }
- };
- MyString operator+(const MyString &lhs, const MyString &rhs)
- {
- cout << "++++++" << endl;
- char *temp;
- strcpy(temp, lhs.GetString());
- strcat(temp, rhs.GetString());
- return MyString(temp);
- }
- // MyString operator+(MyString &&lhs, MyString &&rhs)
- // {
- // }
- MyString operator+(const MyString &lhs, char *rhs)
- {
- char *temp;
- strcpy(temp, lhs.GetString());
- strcat(temp, rhs);
- return MyString(temp);
- }
- MyString operator+(char *lhs, const MyString &rhs)
- {
- char *temp;
- strcpy(temp, lhs);
- strcat(temp, rhs.GetString());
- return MyString(temp);
- }
- // MyString operator+(const char *rhs)
- // {
- // MyString temp();
- // if (*ref == 1)
- // {
- // strcat(str, rhs);
- // return *this;
- // }
- // else
- // {
- // char *temp = str;
- // str = new char[strlen(temp) + strlen(rhs)];
- // strcpy(str, temp);
- // strcat(str, rhs);
- // (*ref)--;
- // delete[] temp;
- // return *(new MyString(str));
- // }
- // }
- ostream &operator<<(ostream &os, MyString &mystr)
- {
- return os << mystr.GetString() << " " << mystr.GetRef();
- }
- ostream &operator>>(ostream &os, MyString &mystr)
- {
- }
- int main()
- {
- MyString a("aaa");
- MyString c(a);
- MyString b("bbb");
- MyString test;
- cout << a << endl;
- cout << b << endl;
- cout << c << endl;
- test = a + b;
- a = test;
- cout << a << endl;
- cout << b << endl;
- cout << c << endl;
- cout << test << endl;
- // MyString a("sssasdasd");
- // cout << a.GetRef();
- // MyString b(a);
- // MyString C(b);
- // if (true)
- // {
- // MyString g = a;
- // cout << endl
- // << g.GetRef() << endl;
- // cout << g << endl;
- // }
- // cout << a << endl;
- // cout << a.GetRef();
- // cout << b << endl;
- // char as[] = "asd";
- // char *sss = as;
- return 0;
- // cout << as;
- // cout << '\n'
- // << sss;
- // sss[2] = '0';
- // cout << '\n'
- // << as;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement