Namespaces

来自cppreference.com

 
 
C + +語言
大會的主題
Original:
General topics
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
流量控制
Original:
Flow control
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
條件執行語句
Original:
Conditional execution statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
迭代語句
Original:
Iteration statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
跳轉語句
Original:
Jump statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
功能
Original:
Functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
函數聲明
lambda函數的聲明
函數模板
的歷史。內嵌說明
異常規範 (過時了)
noexcept說明 (C++11)
例外
Original:
Exceptions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
命名空間
Original:
Namespaces
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
命名空間聲明
類型
Original:
Types
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
decltype specifier (C++11)
規範
Original:
Specifiers
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
CV符
存儲時間說明符
constexpr說明 (C++11)
汽車符 (C++11)
alignas說明 (C++11)
初始化
Original:
Initialization
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Original:
Literals
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
布爾文字
nullptr (C++11)
用戶定義的 (C++11)
表達式
Original:
Expressions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
另一種表示形式
實用工具
Original:
Utilities
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
類型
Original:
Types
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
typedef declaration
聲明類型別名 (C++11)
屬性 (C++11)
施放
Original:
Casts
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
隱式轉換
const_cast conversion
static_cast conversion
dynamic_cast conversion
reinterpret_cast conversion
C-風格和功能轉換
內存分配
Original:
Memory allocation
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Original:
Classes
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
類特定的功能特性
Original:
Class-specific function properties
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
虛函數
覆蓋說明 (C++11)
最後說明 (C++11)
明確的 (C++11)
靜態的
特殊的成員函數
Original:
Special member functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
模板
Original:
Templates
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
類模板
函數模板
模板特化
參數包 (C++11)
雜項
Original:
Miscellaneous
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
內聯彙編
 
命名空間提供了一種方法,以防止在大型工程項目的名稱衝突.
Original:
Namespaces provide a method for preventing name conflicts in large projects.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
的命名空間塊內聲明的符號被放置在指定的範圍內,防止他們被誤認為是相同名稱的符號,在其它範圍內.
Original:
Symbols declared inside a namespace block are placed in a named scope that prevents them from being mistaken for identically-named symbols in other scopes.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
允許多個具有相同名稱的命名空間聲明,導致在一個命名空間包括所有這些聲明中所有的符號.
Original:
Multiple declarations of namespaces with the same name are allowed, resulting in a namespace including all symbols from all such declarations.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

目錄

[编辑] 語法

namespace ns_name { declarations } (1)
inline namespace ns_name { declarations } (2) (C++11 起)
ns_name::name (3)
using namespace ns_name; (4)
using ns_name::name; (5)

[编辑] 解釋

#聲明的命名空間name.
Original:
# Declaration of the namespace name.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
#聲明的命名空間name。定義將同時顯示的內name,包含它的命名空間
Original:
# Declaration of the namespace name. Definitions will appear both inside name and its enclosing namespace
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
#標準方式訪問命名空間中的內容.
Original:
# Standard way of accessing namespace content.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
#所有符號訪問的命名空間的using指令的範圍.....
Original:
# Making all symbols of a namespace accessible in the scope of the using directive.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
#一個特定的符號訪問的命名空間的using指令的範圍.....
Original:
# Making a specific symbols of a namespace accessible in the scope of the using directive.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[编辑] 為例

這個例子展示了如何使用一個命名空間創建一個類,它已經被命名為std命名空間中的.
Original:
This example shows how to use a namespace to create a class that already has been named in the std namespace.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

#include <vector>
 
namespace vec {
 
    template< typename T >
    class vector {
        // ...
    };
 
} // of vec
 
int main()
{
    std::vector<int> v1; // Standard vector.
    vec::vector<int> v2; // User defined vector.
 
    v1 = v2; // Error: v1 and v2 are different object's type.
 
    {
        using namespace std;
        vector<int> v3; // Same as std::vector
        v1 = v3; // OK
    }
 
    {
        using vec::vector;
        vector<int> v4; // Same as vec::vector
        v2 = v4; // OK
    }
 
    return 0;
}


[编辑] 另請參閱

命名空間別名
現有的命名空間創建一個別名
Original:
creates an alias of an existing namespace
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
[edit]