C + + anonymous namespace--anonymous space

Source: Internet
Author: User

C + + anonymous namespace--anonymous space

Anonymous space, anonymous class, anonymous consortium, anonymous struct. Anonymous space
  1. #include <stdio. h>
  2. Namespace A {
  3. int ID = 1;
  4. }
  5. Namespace {
  6. int ID = one;
  7. }
  8. Namespace B {
  9. int ID = +;
  10. }
  11. int main(void){
  12. printf("id%d \ n", id);
  13. }
Output
    1. ID 11
The function of C is used here to reduce the disturbance of STD space. The output here is the contents of the anonymous space. Then it is similar to the following usage
    1. Namespace __unique_name_ {
    2. int ID;
    3. }
    4. using namespace __unique_name_;
If you modify the code as follows
  1. #include <stdio. h>
  2. Namespace A {
  3. int ID = 1;
  4. }
  5. Namespace {
  6. int ID = one;
  7. }
  8. Namespace B {
  9. int ID = +;
  10. }
  11. Namespace {
  12. int ID = a;
  13. }
  14. int main(void){
  15. printf("id%d \ n", id);
  16. }
compile the error, as follows
  1. T_anonymity_space.cpp:14:error:redefinition of ' int:: ID '
  2. T_anonymity_space.cpp:7: Error: ' int:: ID ' previously defined here
In a different file?
    1. File def.h
    2. Namespace {
    3. int ID = a;
    4. }
  1. #include <stdio. h>
  2. #include "Def.h"
  3. Namespace A {
  4. int ID = 1;
  5. }
  6. Namespace {
  7. int ID = one;
  8. }
  9. Namespace B {
  10. int ID = +;
  11. }
  12. int main(void){
  13. printf("id%d \ n", id);
  14. }
Compile error
    1. T_anonymity_space.cpp:7: Error:redefinition of ' int:: ID '
    2. Def.h:3: Error: ' int:: ID ' previously defined here
This shows that anonymous namespace is in a space. References to other spaces later usage
  1. #include <stdio. h>
  2. Namespace A {
  3. int ID = 1;
  4. }
  5. Namespace {
  6. int ID = one;
  7. int id =n;
  8. }
  9. Namespace B {
  10. int ID = +;
  11. }
  12. int main(void){
  13. printf("id%d \ n", id);
  14. using namespace A;
  15. printf("id%d-%d-%d \ n", A:: ID, :: ID, id);
  16. }
Output
    1. ID 11
    2. ID 1-11-12
Other spaces are referenced, and at this point if the use of anonymous space is in the same space, and the same name must be added:: To differentiate. I guess here, is the global variable in the default anonymous space? Judging from the phenomenon, probably is.

C + + anonymous namespace--anonymous space

Related Article

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.