#include <iostream>
#include <iomanip>
#include <cstring>
using namespace std;
class String {
public:
String() : buffer(0U), str(nullptr) {}
String(const String& s) : buffer(s.buffer), str(new char[buffer]) {
copy(str, s.str);
}
String(const char* s) : buffer(strlen(s)), str(new char[buffer]) {
copy(str, s);
}
String(String&& s) {
buffer = move(s.buffer);
str = s.str;
s.str = nullptr;
}
~String() {
if (str != nullptr) {
delete[] str;
str = nullptr;
}
}
String& operator=(const String& s) {
if (&s != this) {
if (str != nullptr) delete[] str;
buffer = s.buffer;
str = new char[buffer];
copy(str, s.str);
}
return *this;
}
String& operator=(const char* s) {
if (str != nullptr) delete[] str;
buffer = strlen(s);
str = new char[buffer];
copy(str, s);
return *this;
}
String& operator=(String&& s) {
buffer = move(s.buffer);
str = s.str;
s.str = nullptr;
str[buffer] = 0;
return *this;
}
private:
size_t buffer;
char* str;
void copy(char* a, const char* b) {
for (auto i = 0U; i < buffer; ++i) a[i] = b[i];
}
friend ostream& operator<<(ostream& out, const String& s) {
for (auto i = 0U; i < s.buffer; ++i) out.put(s.str[i]);
return out;
}
};
int main() {
String hello = "Hello world!";
cout << hello << '\n';
String goodbye = "Goodbye world!";
cout << goodbye << '\n';
String any(goodbye);
goodbye = hello;
cout << goodbye << '\n';
cout << any << '\n';
any = "To be or not to be";
cout << any << '\n';
system("pause > nul");
}