C + + namespaces

Source: Internet
Author: User

I just like the simple expression.


The purpose of using namespaces is to localize the names of identifiers to avoid naming conflicts.

NAMESPACE  We can also create our own namespaces in our own programs so that we can localize names that we think might cause conflicts. This is especially important when we are creating classes or libraries of functions.

Namespace Basics

namespace keywords allow us to delimit global namespaces by creating scopes. Essentially, a namespace defines a scope. The basic form of defining the namespace is as follows:

NAMESPACE  name {// declaration }

Anything defined in a namespace is confined to that namespace.

using key Words

If you need to refer to a member of a namespace more than once in a program, it is cumbersome to use the scope resolver to specify the namespace each time, as we have previously said. To solve this problem, people have introducedusingkey word. usingstatements usually have two ways of using them:

using namespace  namespace name

USING&NBSP; < Span style= "font-family: ' The song Body '; > namespace name < Span style= "Color:rgb (68,68,68);" >:: member;


Reproduced above http://blog.chinaunix.net/uid-26874138-id-3215266.html

=============================================================================

Files: student.h

#include  <iostream>using namespace  std;namespace stu {     class student {        public:              student () {};             ~student () {};             int fun () {                 cout <<  "Stu"  << endl;             }           };} namespace ter {    class student {         public:             student () {};      &nBsp;      int fun ()                 {                        cout <<  "Teacher"  < < endl;            }            };}


File name: Namespace.cxx

#include <stdio.h> #include <iostream> #include "student.h" using namespace std;using namespace stu;using    namespace Ter;int main (void) {stu::student A;     Ter::student b;      A.fun ();    B.fun (); return 0;} ~


Summary: A name for a space, content randomly repeated complementary interference.

This article is from the "Self-summary" blog, please be sure to keep this source http://2714254.blog.51cto.com/2704254/1683760

C + + namespaces

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.