#include <iostream>
#include <cstring>
class String {
private:
char* _buffer;
size_t _size;
public:
String(): _buffer(nullptr), _size(0) {}
String(const char* str)
{
_size = strlen(str);
_buffer = new char[_size+1];
memcpy(_buffer, str, _size+1);
}
~String()
{
delete[] _buffer;
}
String(const String& other): _size(other._size)
{
_buffer = new char[_size+1];
memcpy(_buffer, other._buffer, _size+1);
}
String& operator=(const String& other)
{
if (this == &other) return *this; // handle self-assignment
delete[] _buffer; // free previosly allocated memory
_size = other._size;
_buffer = new char[_size+1];
memcpy(_buffer, other._buffer, _size+1);
return *this;
}
void print()
{
std::cout << _buffer << std::endl;
}
};
int main()
{
String s1("ddd");
String s2;
s2 = s1;
s2.print();
return 0;
}
05-12
2073