Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: C++
Expertise: All
Aug 18, 2000



Building the Right Environment to Support AI, Machine Learning and Deep Learning

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:

cout << "hello world";

Can be rewritten as follows:

cout.operator<< ("hello world");

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.

Danny Kalev
Thanks for your registration, follow us on our social networks to keep up-to-date