You need to define some constructors for the different types that you want to be able to convert into your strings. These constructors can basically just hand the parameters through to the underlying std::string
.
If you don't manually create them, the compiler creates a default- and a copy-constructor for you:
MyString() : std::string() { }
MyString(const MyString &other) : std::string(other) { }
To allow construction from string literals, you need a constructor that takes a const char*
:
MyString(const char* other) : std::string(other) { }
A constructor that takes a const std::string&
would also be useful to convert std::string
s to your string type. If you want to avoid implicit conversions of normal strings, you should make it explicit
:
explicit MyString(const std::string &other) : std::string(other) { }
(Edited because my original version was full of errors and I can't delete the accepted answer)