Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include<cstring>
- using namespace std;
- class _String
- {
- char*data;
- public:
- _String()
- {
- data = nullptr;
- }
- _String(const char*s)
- {
- size_t size = strlen(s) + 1;
- data = new char[size];
- memcpy(data, s, size);
- }
- _String(const _String &other)
- {
- cout << "construct copy\n";
- size_t size = strlen(other.data) + 1;
- data = new char[size];
- memcpy(data, other.data, size);
- }
- _String(_String &&other)
- {
- cout<< "Move copy\n";
- swap(data, other.data);
- other.data = nullptr;
- }
- _String & operator = (const _String & other)
- {
- cout << "Operator = \n";
- if (this == &other) return *this;
- {
- data = new char[strlen(other.data) + 1];
- memcpy(data, other.data, strlen(other.data) + 1);
- return*this;
- }
- }
- _String operator = (_String && other)
- {
- cout << "Move \n";
- swap(data, other.data);
- return *this;
- }
- _String operator + (const _String & other)
- {
- cout << "operat + \n";
- _String temp;
- temp.data = new char[strlen(data) + strlen(other.data) + 1];
- memcpy(temp.data, data, strlen(data));
- memcpy(temp.data + strlen(data), other.data, strlen(other.data) + 1);
- return temp;
- }
- void Print()
- {
- cout << data << endl;
- }
- ~_String()
- {
- cout << "deletor\n";
- delete[] data;
- }
- };
- void main()
- {
- _String s1("Hello "), s2("world"), s3;
- s3 = s1 + s2;
- s3.Print();
- _String s4(s3);
- system("Pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement