Using instructions and using compilation instructions

Source: Internet
Author: User

Using declaration:

Using JILL: A; // equivalent to defining variables in the region

Using compilation command:

Using namespace Jill; // although defined in this region, it takes effect globally

Using declaration.

# Include <iostream> using namespace STD; namespace Jill {int A;} int main (INT argc, char * argv []) {using Jill:; // int A = 3; an error will occur, because the above definition is equivalent to a (redefinition error) cout <A <Endl; return 0 ;}

Because it is in the same region, it is equivalent to two int A; therefore, a redefinition error occurs.

You can change it to the following:

#include <iostream>using namespace std;namespace jill{int a;}using jill::a;int main(int argc, char *argv[]){int a=3;cout<<a<<endl;return 0;}

In this way, they are not in the same region, and the following a overwrites the global

Using compilation command:

# Include <iostream> using namespace STD; namespace Jill {int A;} int main (INT argc, char * argv []) {using namespace Jill; // although in this region, however, it is a global int A = 3; // Therefore, The Global is overwritten here, And cout <A <Endl; return 0 ;}

As mentioned above, functions do not have errors:

# Include <iostream> using namespace STD; namespace Jill {int A;} using namespace Jill; int main (INT argc, char * argv []) {int A = 3; // overwrite the global data. cout <A <Endl; return 0 ;}

What is the difference between this example and the previous one?

Although they are all "Global", they have different scopes. The previous scope is only within Main. This is the entire file.

Well, it's a bit strange, but the global effect is in different regions.

In this case, someone made a note, which is excerpted as follows:

Http://hi.baidu.com/wangxiaoliblog/item/9bb7fdc5ed035bc0994aa06a

C ++ provides these two mechanisms to simplify the use of namespaces,

The Using Declaration makes specific identifiers available, and the using compilation Command makes the entire namespace available.


1. The using Declaration consists of a qualified name and the keyword using, for example:
Using JILL: Fetch;
2. The using Compilation instruction is composed of the namespace name and the keyword using nameapace before it. It makes all the names in the namespace available without the scope resolution OPERATOR:
Using namespace Jack; // make all the names in Jack available


3. Differences between the two mechanisms:

#include <iostream>using namespace std;namespace Jill{double fetch;}char fetch;int main(){using Jill::fetch;double fetch;   //error! redefinedcin >> fetch;cin >> ::fetch;return 0;}

Note that the using Declaration defines a fetch, so a redefinition error occurs.

Int main () {using namespace Jill; double fetch; // OK, overwrite fetchcin> fetch; CIN >>:: fetch; CIN >> JILL: Fetch; return 0 ;}

Note: using the using compilation command to import all the names in a namespace is different from using multiple using declarations, and more like using a large number of scope resolution operators.

When using declaration, it is like declaring the corresponding name. If a name already exists in the function, the same name cannot be imported using the using declaration.

In the preceding example, The namespace is global. If you use the using compilation command to import a name already declared in the function, the local name will hide the namespace name,

Just like hiding a global variable with the same name. However, the scope parsing operator can still be used as in the last CIN sentence above.

(The book is so vague)

Remember:


1. Assume that the namespace and the declared region have the same name. If you try to use the using declaration to import the namespace name to the declared region, the two names will conflict and thus cause an error. If you use the using compilation command to import the namespace name to the declared area, the local version will hide the namespace version.


2. In general, using declaration is safer than using to compile commands, because it only imports the specified name. If the name conflicts with a local name, the compiler will give instructions. The Using compilation command imports all names, including names that may not be needed. If it conflicts with a local name, the local name will overwrite the namespace version, and the compiler will not issue a warning. In addition, the openness of the namespace means that the namespace names may be scattered in multiple places, which makes it difficult to know which names are added accurately.

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.