Does the overloading of the assignment operator propagate to an initializer list?
For example, suppose a class:
class MyClass {
private:
std::string m_myString; //std::string overloads operator =
public:
MyClass(std::string myString);
}
And a constructor:
MyClass::MyClass(std::string myString)
: m_myString(myString)
{
}
Will the initializer list work out the assignment operator overload on std::string? And if not, is there a workaround?
Particularly for GCC.
I think what you are missing is the difference between
assignmentandinitialization.Lets look at a simple example with a fundamental type:
The above example is simple and not difficult to understand. However, when you get into user-defined types, it is not as simple because objects are constructed.
For example, lets look at
std::stringThe key thing here is
operator=means different things in different contexts. It all depends on what is on the left hand side.And initializer lists do initialization only (hence the name initializer list).
Just be aware, when you call
operator=in the body of the constructor, you are now doing assignment and not initialization.