C + + variables are visible within nested fields

Source: Internet
Author: User

Each name in a C + + program must point to a unique entity (such as an object, function, type, or template). But we all know that in a program, the same name often appears pointing to different entities. The mechanism for this implementation is that the compiler has different meanings for names based on the context area. The general context used to distinguish the meaning of a name is the domain (scope). C + + supports three forms of domain: Local domain, namespace domain (namespace scope), and class domain.

For example, in a CPP file, there is a global variable, the relative function is the global domain nested domain, this global variable is visible in all nested fields.
Also like class, there is another class, then the outer layer of the variable in the class is also visible:

#include <iostream>using namespace std; class c1{public:    int a;    void foo();    class c2    {    public:        int a;        void foo();    } b;};void c1::foo(){    a = 1;}void c1::c2::foo(){    a = 2;} int main(){     class c1 f;    f.foo();    f.b.foo();    cout << f.a << endl;    cout << f.b.a << endl;    return 0;}

C + + variables are visible within nested fields

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.