Invoking Overloaded Operators Explicitly

Invoking Overloaded Operators Explicitly

The overloaded operator mechanism is “syntactic sugar” for ordinary function calls. You can always use the explicit name of an overloaded operator function to resolve ambiguities or document your intention. For example, the following statement:


Can be rewritten as follows:


Instead of using the operator's sign directly, you can use a combination of the keyword 'operator' followed by the operator sign and its argument list enclosed in parentheses. Remember to place a dot after the object's name or -> after a pointer when you call an overloaded operator function this way:

 string p = new string;bool = empty = p->operator==(""); // using explicit call

Although you wouldn't use this unwieldy and cumbersome form normally, you should be familiar with this notation because some compilers and linkers issue error messages that contain the explicit operator function name.


Share the Post: