Namespaces can also be customized

Source: Internet
Author: User

Definition of namespaces

namespaces, in addition to the system-defined namespaces, can be defined by themselves, defining namespaces with the keyword "namespace", using the namespace with the symbol "::" specified.

A variable or function that does not specify a namespace is a variable or function under the current namespace.

If you do not define a namespace, all of them belong to the global namespace.

The same namespace can be defined multiple times.

namespace definitions and usage examples:

#include <iostream>

using namespace Std;

Namespace NA

{

void print (int n) {//is the same as the function name of the namespace NB

cout << "na::p rint:" << n << Endl;

}

}

Namespace NB

{

void print (int n) {//is the same as the function name of namespace NA

cout << "NB::p rint:" << n << Endl;

}

}

Namespace na//the same namespace can be defined multiple times

{

void Print2 (int a, int b) {

Print (a);

Print (b);

}

}

int main ()

{

NA::p rint (3); Specify the print function under NA

NB::p rint (4); Specify the print function under NA

NA::p rint2 (5, 6); Specify the Print2 function under NA

return 0;

}

Operation Result:

NA::p rint:3

NB::p rint:4

NA::p rint:5

NA::p rint:6

Use of using

Using the "using namespace" allows you to specify the namespace at a later time, which is equivalent to the Import function in Java. However, when using the "using namespace" multiple times, the namespaces that are specified are valid and conflict-prone.

#include <iostream>

using namespace Std;

Namespace NA

{

void print (int a) {

cout << "na::p rint:" << a << Endl;

}

}

Namespace NB

{

using namespace na;

void Print2 (int a, int b) {//This is the function of NB itself

Print (a); Functions of NA

Print (b); Functions of NA

}

void print (int n) {//This is also the function of NB itself

cout << "NB::p rint:" << n << Endl;

}

}

Namespace NC

{

Using NB::p rint2; Specifies that only NB's Print2 () function is used

void print (int a, int b, int c) {

Print2 (A, b); This is the function of NB.

NB::p rint (c); The using is not specified, so you must use NB:: To specify

}

}

int main ()

{

using namespace na;

Print (2); The print () function of NA

using namespace NB; When used in this way, NA and NB are available simultaneously

Print (3); The compilation system is not sure which of Na and NB to call, because the function name and parameters are the same

using namespace NC; Na and NC are also valid when used in this way

Print (4, 5, 6); The parameters of the NC print () function are different from the parameters in Na, so there is no error here.

Using NB::p rint; Specifies that the following print () function is a function of NB

Print (7);

return 0;

}

Operation Result:

NA::p rint:2

NA::p rint:4

NA::p rint:5

NB::p rint:6

NB::p rint:7

Specify namespaces with aliases

Namespaces can be nested layers, specify when you have to specify a layer, so it is inconvenient, the general use of aliases instead of more convenient.

#include <iostream>

using namespace Std;

Namespace Na {

Namespace NB {

Namespace NC {

int sum (int a, int b)

{

return a + B;

}

}

}

}

namespace A = Na::nb::nc;

int main ()

{

cout << Na::nb::nc::sum (5, one) << Endl;

cout << A::sum (6, a) << Endl;

return 0;

}

Operation Result:

16

18

namespaces without a name

Namespace names can also be omitted, known as "Nameless namespace", nameless namespace is often used, its role is "internal can be referenced, and external cannot reference."

#include <iostream>

using namespace Std;

Namespace Na {

namespace {//unknown namespace

int sum (int a, int b)

{

return a + B;

}

}

int calc (int x, int y)

{

return sum (x, y); Inside a variable or function that can invoke the nameless namespace

}

}

int main ()

{

cout << Na::sum (5, one) << Endl; Because the name inside is missing, the external cannot be called

cout << Na::calc (6, a) << Endl;

return 0;

}

Operation Result:

18

Namespaces can also be customized

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.