Operators in namespaces

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.