Consider the binary operator @, if x is of type x, and y is of type Y,[email protected] will be parsed as follows:
--If X is a class, query as a member function of x or a member function of a base class of x [email protected].
--in the context of [email protected], check the [email protected] statement.
--if x is defined in namespace n, query [email protected] declaration in N.
--in the definition of Y in the namespace m, in the M-Cha village [email protected] statement.
1#include <iostream>2 using namespacestd;3 4 classX {5 Public:6 void operator+(int) {7cout <<"From class X"<<Endl;8 }9 };Ten One intMain () { A x x; -X +1;//From class X - the return 0; -}For Item 1
1#include <iostream>2 using namespacestd;3 4 classX {5 Public:6 };7 8 classY {9 Public:Ten }; One A - intMain () { - x x; the y y; - - void operator+ (x& X, y&y); - +x + y;//From function - + return 0; A } at - void operator+ (x& X, y&y) { -cout <<"From function"<<Endl; -}For Item 2
1#include <iostream>2 using namespacestd;3 4 namespaceNX {5 classX {6 Public:7 /*void operator+ (int) {8 cout << "from class X" << Endl;9 }*/Ten }; One A void operator+ (x& X,int) { -cout <<"Form namespace NX"<<Endl; - } the } - - intMain () { - nx::x X; +X +1;//From namespace NX - + return 0; A}For Item 3
1#include <iostream>2 using namespacestd;3 4 classX {5 Public:6 };7 8 namespaceNY {9 classY {Ten Public: One }; A - void operator+ (x& X, y&y) { -cout <<"From namespace NY"<<Endl; the } - } - - intMain () { + x x; - ny::y Y; + Ax + y;//From namespace NY at - return 0; -}For Item 4
Reference: TC++PL P237
Operators in namespaces