C ++ namespace domain

Source: Internet
Author: User
Tags ranges
Autocrat

C ++ namespace <to>

Familiar with the C/C ++ language, familiar with the Windows development platform, skilled in using the MFC self-editor to develop some applications;
Familiar with SQL statements, good understanding of databases, and skillful use of SQL Server2000 software;
Be familiar with the Java language, and be familiar with the foundation of the Development of Mobile Phone software by using Java;
Gain an in-depth understanding of object-oriented ideas and be proficient in specific programming and development;
Familiar with C language programming and common commands in Unix/Linux, and assembly language;
Familiar with network protocols such as TCP/IP and UDP, able to handle common faults in computer system software;

 

C ++ using namespace STD details
Namespace refers to various visible ranges of identifiers. All identifiers in the C ++ standard library are defined in a namespace named STD.
I:

<Iostream> and <iostream. h> It is different. The former has no suffix. In fact, you can see in the include folder of your compiler that the two are two files. When you open the file, you will find that the code inside is different.

Suffix. h header file C ++ standards have been explicitly proposed not to support, earlier implementation of the standard library function defined in the global space, declared in the. in the header file with the suffix H, the C ++ standard is used to distinguish it from C, and to correctly use the namespace, it is required that the header file does not use the suffix. h.

Therefore, when <iostream. h> when <iostream> is used, it is equivalent to calling the library function in C and using a global namespace, that is, the early C ++ implementation, the header file does not define a global namespace. You must use namespacestd to use cout correctly.

II:

Namespace refers to various visible ranges of identifiers.

All identifiers in the C ++ standard library are defined in a namespace named STD.

Due to the concept of namespace, when using any identifier of the C ++ standard library, there are three options:

1. Specify the identifier directly. For example, STD: ostream instead of ostream. The complete statement is as follows:

STD: cout <STD: Hex <3.4 <STD: Endl;

2. Use the using keyword.

Using STD: cout;
Using STD: Endl;

The above program can be written

Cout <STD: Hex <3.4 <Endl;

3. the most convenient method is to use using namespace STD;

For example:

# Include <iostream>
# Include <sstream>
# Include <string>
Using namespace STD;
In this way, all identifiers defined in the namespace STD are valid (exposed ). They are declared as global variables. The preceding statement can be written as follows:

Cout

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 STD encapsulates the name of the standard library. to distinguish the standard library from the previous header files, ". H" is generally not added"

 

 

Using namespace STD usage

From

Using namespacestd; 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.

Try not to use using namespace STD; feel for using VC ++ 2005
Posted on Samson Day (1163) Comments (6) EDIT favorites category: C ++/C ++. net
Today, I used Visual C ++ to write a small program (vs2005), which is very simple, but compilation fails.
There is a strange problem: Error 1 error c2668: "Max": the call to the overloaded function is not clear.

The initial code is as follows:

# Include <iostream>
Using namespace STD;

Template <typename T>
T max (t a, t B)
{
Return (A> B )? A: B );
}
Void main ()
{
Double X, Y;
Cin> x> Y;
Cout <"Max number is" <(max (x, y) <Endl;
Cin> X;
}

   I put this code under VC ++ 6.0 and passed it. The program runs normally. This leaves me puzzled. Then I finally figured it out!
   In fact, there is a max function in the STD namespace, and the functions are the same ...... I'm dizzy. By using the definition function, you can see how Microsoft writes the max function. In order not to be despised, Microsoft's code will not be pasted here.
   To understand why this error occurs, we can rewrite the Code as follows:
# Include <iostream>
Using STD: CIN;
Using STD: cout;
Using STD: Endl;

Template <typename T>
T max (t a, t B)
{
Return (A> B )? A: B );
}
Int main ()
{
Double X, Y;
Cin> x> Y;
Cout <"Max number is" <(max (x, y) <Endl;
Cin> X;
}
   This is my recommended practice, because C ++ primer and valid tive C ++ use this method, but tan haoqiang's book is a usingnamespace STD; that's it, I thought it was simple and I used it all the time. I didn't expect it to bring so many problems. In the past, I encountered an inexplicable error in youyuanfunction.
   In fact, there are two simple solutions, that is, to change the defined function to another name, or directly use the function provided by Microsoft. I believe that the efficiency provided by Microsoft will never be lower than what we write ~
   Well, you can write it here. I hope you will develop good programming habits. ^-^

Many C ++ programmers are still using standard libraries instead of updating them.
What are the differences between the two? First, five years ago, we began to oppose the use of the. h symbol in the standard header.
File. It is not a good way to continue using outdated rules. In terms of functionality,
<Iostream> contains a series of templated I/O classes. On the contrary, <iostream. h> only supports characters.
Stream. In addition, the C ++ standard interface of the input/output stream has been improved in some subtle details. Therefore,
<Iostream> and <iostream. h> are different in terms of interfaces and execution. Finally, each group of <iostream>
Chengdu is declared as STL. However, the components of <iostream. h> are declared as global.

Because of these differences, you cannot confuse these two libraries in a program. As a kind of study
<Iostream> is generally used in new Code. However, if you are dealing with code written in the past
Inheritance can be used to continue to use <iostream. h> to maintain code consistency.

 

///////////////////

<Iostream> indicates that you are using the annotation namespace, that is, there should be such a sentence at the beginning of the program.
Using namespace STD;
This follows the C ++ standard.
<Iostream. h>
The C ++ standard is not followed.
////////////////
<String. h> is the old c header file, which corresponds to a char *-based string processing function;
<String> the C ++ header file of STD is packaged, corresponding to the new strng class;
<Cstring> is the STD Version corresponding to the old c header file.

In a program written in C ++, the range of variables and functions is limited. For example, a temporary variable defined in the function body cannot be used in vitro. To solve the scope of variables and functions, the concept of namespace is introduced in C ++, And the keywords namespace and using are added. 
         A group of variables and functions can be defined in a namespace. These variables have the same scope as the function. These variables and functions can be called members of the namespace. 
         Through namespace, you can use the same variable name or function name in the same file, as long as they belong to different namespace. In addition, namespace allows code operations to have variables with the same name but different libraries. Furthermore, namespace can improve the compatibility between C and C ++. 
   
The following example describes the usage of the keyword namespace. 
# Include <Conio. h> 
# Include <Iostream. h> 
Namespace Car // Namespace Definition 

     Int Model; 
     Int Length; 
     Int Width; 

   
Namespace Plane 

     Int Model; 
     Namespace Size // Namespace nesting 
     { 
         Int Length; 
         Int Width; 
     } 

   
Namespace Car // Add a namespace Member 

     Char * Name; 

   
Namespace C = car; // Define the alias of a namespace 
Int Time; // External variables belong to the global namespace 
   
Void Main () 

     Car: length = 3; 
     // The following error is blocked. 
     // Width = 2; // The namespace should be specified for non-global variables and currently valid temporary variables 
     Plane: Size: length = 70; 
     Cout <" 

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.