#include #include #include #include // strlen strdup strcpy strncpy strcat #define T printf("%s body='%s'\n", __PRETTY_FUNCTION__, body) class str { public: str(const char *val = "") { body = strdup(val); T; } ~str() { T; free(body); } void print() const { printf("[%s]\n", body); } str & operator= (const str &b) { free(body); body = strdup(b.body); T; return *this; } str(const str &b) { body = strdup(b.body); T; } str operator+ (const str &b) const { T; size_t need = strlen(body) + strlen(b.body) + 1; str ret(""); ret.body = (char *)realloc(ret.body, need); assert(ret.body != nullptr); strcpy(ret.body, body); strcat(ret.body, b.body); return ret; } private: char *body; // C-string, null-terminated "123" -> '1', '2', '3', 0 }; void foo(str c) { printf("foo!"); c.print(); } int main() { str a("abra"); str b("shvabra"); str c = a + b; str e(a+b); a.print(); b.print(); c.print(); c = a; c.print(); str d = a; d.print(); // str c = a + b; }