14.4. Assignment Operators
In addition to the copy- and move-assignment operators that assign one object of the class type to another object of the same type (§ 13.1.2, p. 500, and § 13.6.2, p. 536), a class can define additional assignment operators that allow other types as the right-hand operand.
As one example, in addition to the copy- and move-assignment operators, the library vector class defines a third assignment operator that takes a braced list of elements (§ 9.2.5, p. 337). We can use this operator as follows:
vector<string> v;v = {"a", "an", "the"};
We can add this operator to our StrVec class (§ 13.5, p. 526) as well:
class StrVec {public: StrVec &operator=(std::initializer_list<std::string>); // other members as in § 13.5 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access