The second example isn't calling operator=
, it's calling a conversion constructor for const char []
, or whatever you'd be using it for internally, as long as it can convert from that (e.g. std::string
), which doesn't exist as of yet. You can see one implemented in std''OrgnlDave's answer. It's almost identical to
MyClass var2 ("string");
The latter, though, is explicit, whereas the former is implicit. To see the difference, make a constructor and mark it explicit
. The code here will work, but yours won't. This can save confusion when you, for example, pass a string by accident instead of a MyClass
, and it gets implicitly converted when it isn't even meant to be a MyClass
in the first place.