C + + Namespace

Source: Internet
Author: User

A: And is not the same, the former has no suffix, in fact, in your compiler include folder can be seen, the two are two files, open the file will find that the code is not the same. Header file c with suffix. hThe + + standard has been explicitly not supported, and earlier implementations defined the standard library functionality in the global space, declared in the header file with the. h suffix, C + +The standard is to differentiate from C, and also to properly use namespaces, to specify that the header file does not use a suffix. h. Therefore, when used, it is equivalent to calling library functions in C, using the global namespace, which is the earlier C++when used, the header file does not have a global namespace defined, and namespace STD must be used so that cout can be used correctly. Second: The so-called namespace refers to the various visible ranges of identifiers. All identifiers in the C + + standard library are defined in a namespace called Std. Due to the concept of namespace, there are three options for using any identifier of the C + + standard library:1, specifying the identifier directly. For example Std::ostream rather than ostream. The complete statement is as follows: Std::cout<< Std::hex <<3.4<<Std::endl;2, use the Using keyword. usingstd::cout;usingStd::endl; The above procedure can be written cout<< Std::hex <<3.4<<Endl;3, the most convenient is to use usingnamespacestd; For example: #include #include#includeusing namespacestd; All identifiers defined within the namespace Std are valid (exposed). As if they were declared as global variables. Then the above statement can be written as follows: Cout<< Hex <<3.4<<Endl, because the standard library is very large, the programmer in the choice of the class name or function name is likely to be the same name in the standard library. So in order to avoid the name conflict caused by this situation, everything in the standard library is placed in the namespace Std. But this poses a new problem. Countless original C++The code relies on functionality in pseudo-standard libraries that have been used for years, and they are all in the global space. So there's a header file like that and so on, one to be compatible with the previous C++code, one is to support the new standard. Namespace STD Encapsulation is the name of the standard library, standard library in order to be different from the previous header file, generally do not add". h"===================namespacename {Declaration-list; }thenamespaceKeyword allows you to create aNewScope. The name isOptional, and can be omitted to create a unnamednamespace. Once you create anamespace, you'll has to refer to it explicitly or use the Using keyword. Example Code:    namespaceCartoonnamespace {intHomersage; voidIncrementhomersage () {homersage++; }       }       intMain () {... Cartoonnamespace::homersage= the;           Cartoonnamespace::incrementhomersage (); cout<< Cartoonnamespace::homersage <<Endl; ...       } Anonymousnamespace    namespace{Declaration-list; }namespacealiasnamespaceidentifier =namespace-specifier can be divided into different sections of code #include<iostream>#include<string>using namespacestd; namespacezoo{classanimal{Private:                            intlegs;  Public: Animal (): Legs (4){}                            voidSayvoid) {cout<<"Legs:"<< Legs <<Endl;    }            }; }    namespacezoo{classplant{Private:                            stringname;  Public: Plant (): Name ("a_plant") {} plant (stringNM): Name ("a_plant") {Name=nm; }                            voidSayvoid) {cout<<"Name:"<< name <<Endl;    }            }; }    intMainvoid) {Zoo::animal dog;            Dog.say (); /*error * ' Animal ' is not declared in this scope animal cat;             Cat.say (); */Zoo::p lant Flower ("Flower");    Flower.say (); } Output Legs:4Name:flower Reference: http://www.kuqin.com/language/20080107/3532.htmlhttp://www.cppreference.com

C + + Namespace

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.