I would completely avoid the char pointer and use std :: string. This way, you don’t even need a copy constructor and an assistant operator, because a compiler generated once will work just fine. (because the "elements" of the "Set" class are copyable and have an assignment operator) Here is my solution:
#include <iostream> #include <string> class Set{ std::string elements; public: Set() { elements = ""; } explicit Set(char* _elements) { if (_elements) elements = _elements; } Set operator+(const Set& s){ Set temp(*this); temp.elements += s.elements; return temp; } };
Btw. I added a constructor from char *, so that the "elements" can be somehow initialized from the outside. Not sure if this is what you wanted.
source share