fork download
  1. #include <iostream>
  2. #include <cstring>
  3.  
  4. using std::strlen;
  5.  
  6. struct string
  7. {
  8. char *buffer;
  9. std::size_t len;
  10.  
  11. string() : buffer(nullptr), len(0)
  12. {}
  13. string(char const *str) : buffer(new char[strlen(str) + 1]), len(strlen(str))
  14. {
  15. std::strcpy(buffer, str);
  16. }
  17. string(string const& other) : buffer(other.data()), len(other.size())
  18. {}
  19.  
  20. string& operator +=(string const& other)
  21. {
  22. std::strcat(buffer, other.data());
  23.  
  24. return *this;
  25. }
  26.  
  27. char *data() const { return buffer; }
  28.  
  29. std::size_t size() const { return len; }
  30. };
  31.  
  32. std::ostream& operator <<(std::ostream& os, string const& str)
  33. {
  34. return os << str.data();
  35. }
  36.  
  37. int main()
  38. {
  39. string s1 = "Hello", s2 = " World";
  40.  
  41. s1 += s2;
  42.  
  43. std::cout << s1;
  44. }
Success #stdin #stdout 0s 2984KB
stdin
Standard input is empty
stdout
Hello World