C ++ namespaces and user functions

Source: Internet
Author: User

Recently, a magic compile error occurred while using the namespace and the youyuan function. After thinking, the problem was finally solved. I hope it will be helpful to you.

Let's talk about namespaces first. When writing a C Project, especially a program with more than 10 million lines, namespace conflicts are a very crashing thing. Currently, the most widely used solution is to make the function name very long and learn about GTK ). In C ++, a feature called "namespace" is added, which is declared, defined, and used by the keyword "namespace. I believe that students who have studied C ++ have a deep understanding of how to use the namespace. I will not introduce it here.

When we sometimes need to overload some operators of the class, it is difficult to avoid the use of the "friend function ". Once upon a time, Daniel criticized the encapsulation of the "youyuan function" damage class. However, for low-level coder like me, the "Friends function" does bring a lot of convenience.

Question 1:

Let's look at a simple code:

 
 
  1. class istream; 
  2. istream& operator>>(istream&,int&); 
  3. class CA 
  4.     int a;
  5.     friend istream& operator>>(istream&,CA& ); 
  6. }; 
  7. istream& operator>>(istream& is,CA& a) 
  8.     is>>a.a; 
  9.     return is; 
  10. }
  11. int main() 
  12.    extern istream stream; 
  13.    elephant::CA a; 
  14.    stream>>a;
  15. }

Save as a. cpp and compile it without link ).

G ++-c a. cpp

Because there is no definition of the istream class, the link will certainly fail. Use the-c option to compile without link.

At this time, there is no problem with compilation.

When I want to include the CA class in an elephant command space, a problem occurs. Use code to speak:

 

 
 
  1. class istream; 
  2. istream& operator>>(istream&,int&); 
  3. namespace elephant 
  4.     class CA 
  5.     { 
  6.         int a;
  7.         friend istream& operator>>(istream&,CA& ); 
  8.     }; 
  9. istream& operator>>(istream& is,elephant::CA& a) 
  10.     is>>a.a; 
  11.     return is; 
  12. int main() 
  13.    extern istream stream; 
  14.    elephant::CA a; 
  15.    stream>>a;
  16. }

Similarly, only compilation, no link, and an error is reported:

# G ++-c a. cpp
A. cpp: In function 'istream & operator> (istream &, elephant: CA &)':
A. cpp: 7: error: 'int elephant: CA: a' is private
A. cpp: 13: error: within this context

What do you mean? A is a private member and cannot be accessed. I made you friend. You told me you couldn't access it. Why?

Let's try to modify the code and add operator> function to the elephant namespace, that is, change row 11th:

 

 
 
  1. istream& elephant::operator>>(istream& is,elephant::CA& a) 

Compile only, do not link, report error:

# G ++-c a. cpp
A. cpp: 11: error: 'istream & elephant: operator >>>( istream &, elephant: CA &) 'Could have been declared inside 'elephant'

It said that this function should be declare in the elephant namespace. I believe you can distinguish the difference between declare and define. The first one is "Declaration", and the other one is "Definition "). Then we will declare. The complete code is as follows:

 
 
  1. class istream; 
  2. istream& operator>>(istream&,int&); 
  3. namespace elephant 
  4.     class CA 
  5.     { 
  6.         int a;
  7.         friend istream& operator>>(istream&,CA& ); 
  8.     }; 
  9.     istream& operator>>(istream&,CA&); 
  10. istream& elephant::operator>>(istream& is,elephant::CA& a) 
  11.     is>>a.a; 
  12.     return is; 
  13. int main() 
  14.     extern istream stream; 
  15.     elephant::CA a; 
  16.     stream>>a; 

Compile only, not link. Compiled successfully.

Summary:

1. friend does not declare the function, so we need to declare this function separately. Friend only tells this class that this function has access permissions to Private Members of this class.

2. The declared functions in the namespace have been included in the namespace. The namespace specifier should be used for definition.

3. friend only shows that the elephant: operator> function is a CA-like membership function, while the operator> function is not a membership function like CA. Therefore, the compiler reports an error if operator> operator class CA is a private member.

4. elephant: operator> and operator> are not a function. The former is a function defined in the elephant namespace, while the latter is a global function.

 

Question 2:

Or a simple code:

 

 
 
  1. void output(int a); 
  2. class SA 
  3.     int data; 
  4.     friend void output(const SA&); 
  5. }; 
  6. void output(const SA& a) 
  7.     output(a.data); 
  8. int main() 
  9.     SA a; 
  10.     output(a); 

Compile only, not link. No problem. Now, similarly, I want to put the class SA in the elephant namespace. According to question 1, we also need to put the output function in the elephant namespace. The Code is as follows:

 

 
 
  1. namespace elephant 
  2.     class SA 
  3.     { 
  4.         int data; 
  5.         friend void output(const SA&); 
  6.     }; 
  7.     void output(int a); 
  8.     void output(const SA&); 
  9. void elephant::output(const elephant::SA& a) 
  10.     output(a.data); 
  11. int main() 
  12.     elephant::SA a; 
  13.     elephant::output(a); 

Compile only, not link. No problem.

However, due to some special needs, I want the output function and all its overload functions to be defined in the global space. What should I do?

I tried it twice. The first attempt failed. The code above:

 
 
  1. void output(int a); 
  2. namespace elephant 
  3.     class SA; 
  4. void output(const elephant::SA&); 
  5. namespace elephant 
  6.     class SA 
  7.     { 
  8.         int data; 
  9.         friend void output(const SA&); 
  10.     }; 
  11. void output(const elephant::SA& a) 
  12.     output(a.data); 
  13. int main() 
  14.     elephant::SA a; 
  15.     output(a); 

Because the output function requires elephant: SA type parameters, a forward declaration is made for elephant: SA. However, obviously, this is not acceptable. According to question 1, elephant: output has access permissions to the private members of elephant: SA, but output does not.

How can this problem be solved? Or, how to declare the output function of the global space as the friend function of elephant: SA. A: Use the global space specifier. Code:

 
 
  1. void output(int a); 
  2. namespace elephant 
  3.     class SA; 
  4. void output(const elephant::SA&); 
  5. namespace elephant 
  6.     class SA 
  7.     { 
  8.         int data; 
  9.         friend void ::output(const SA&); 
  10.     }; 
  11. void output(const elephant::SA& a) 
  12.     output(a.data); 
  13. int main() 
  14.     elephant::SA a; 
  15.     output(a); 

Compile only, not link. Successful.

":" Is preceded by a space, which is called a global space descriptor. I believe you have learned C ++ before. I will not explain it in detail here.

Summary:

1. When you define a class membership function in a namespace, It defaults to a function in the same namespace.

2. If you want to point to a function in the global space, use the global space specifier.

 

 

Related information:

Operating System: ubuntu linux 10.10 maverick

Compiler: g ++ version 4.4.5 (Ubuntu/Linaro 4.4.4-14ubuntu5)

 

This article is from the "elephant_liu" blog, please be sure to keep this source http://elephantliu.blog.51cto.com/1107116/610844

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.