Using namespace STD usage

Source: Internet
Author: User

 

 

Using namespace STD; many of them are used!
---------------------------------------------------------------

Actually, it is to tell the compiler where to find your type.

Using namespace STD is commonly used, that is, the standard namespace of C ++ is used.

You can also reference your own namespace. For example:

Import "C: // mytest // test. TLB"
Using namespace cmytest

You can reference each type name in cmytest.

View C ++ prime
---------------------------------------------------------------

Declare that this file uses the C ++ standard library!
For example
# Include <iostream>
Using namespace STD;
Void main ()
{
Cout <"Hello! "<Endl;
}

If you do not need using namespace STD ;,
STD: cout <"Hello! "<Endl;
This is a namespace issue! For details, see related books. The new C ++ books should all be introduced!

---------------------------------------------------------------

Using indicator!
This is a namespace issue and a new concept introduced by standard C ++!
For details, see section 8.6 of C ++ primer!
---------------------------------------------------------------

Because the standard library is very large, the programmer may have the same name as a standard library when selecting the class name or function name. To avoid name conflicts caused by this situation, everything in the standard library is put in the namespace STD. However, this poses a new problem. Countless original C ++ Code relies on features in the pseudo-standard library that have been used for many years. They are all in the global space.

Therefore, the header files such as <iostream. h> and <iostream> are designed to be compatible with the previous C ++ code, and to support new standards.
---------------------------------------------------------------

The namespace is actually used to make it easier for programs to run correctly on different platforms.
---------------------------------------------------------------

Namespace is introduced to solve name conflicts in C ++.
What is name conflict? For example, there is a class myclass in file X. H,
In the file Y. h, there is also a class myclass, while in the file Z. cpp
References X. h and Y. H files. Obviously, the common method is not feasible,
What should we do? Introduce namespace. For example:
The content in X. H is
// X. h
Namespace mynamespace1
{
Class myclass
{
Public:
Void F ();
PRIVATE:
Int m;
}
};

The content in Y. H is
// Y. h
Namespace mynamespace2
{
Class myclass
{
Public:
Void F ();
PRIVATE:
Int m;
}
};

Then introduce X. h and Y. h In Z. cpp.
// Z. cpp
# Include "x. H"
# Include "Y. H"

Void Z: F ()
{
// Declare an instance of myclass in file X. h.
Mynamespace1: myclass X;
// Declare an instance of myclass in file X. h.
Mynamespace2: myclass y;

// Call function f in file X. h
X. F ();
// Call function f in file Y. h
Y. F ();
}
A namespace is essentially a scope.
The above instance should know the role of the 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.