-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Modern C++ For C Programmers #11
Comments
Modern C++ For C Programmers Part 1
|
Modern C++ For C Programmers Part 2Namespaces大型程式常需要使用各種獨立開發的函式庫,無可避免地,某些命名會相衝突,namespace 提供一個很好的機制來避免名稱衝突,透過 namespace 可以將函式、變數等等歸類在某個 scope 中(像是分成不同組別),不同群組間的同名稱便不會相衝突。 Defining Namespace Members在同一個 namespace 底下的東西可以分開來宣告或定義,像是在 #include "Sales_data.h>"
namespace cplusplus_primer {
std::istream& operator>> (std::istream& in, Sales_data& s) { /* ... */ }
} 也可以不要在 namespace definition 外去定義: cplusplus_primer::Sales_data
cplusplus_primer::operator+ (const Sales_data& lhs, const Sales_data& rhs) {
Sales_data ret(lhs);
// ...
} 一旦看到全名(也就是 Inline NamespacesC++ 11 引進的新功能,可以作為某種 nested namespace: namespace cplusplus_primer {
inline namespace FifthEd {
// ...
}
namespace Fourth {
// ...
}
} 像上面的 Unnamed NamespacesUnnamed namespace 底下的變數具有 static lifetime。這裡的 static 特性只限於同個檔案中,不會跨檔案,也就是說假設某標頭檔有 unnamed namespace,若有 A, B 兩個檔案去引入此標頭檔,裡頭的變數在兩個檔案之中是不同的實體(可以想像成 local 跟 global 的差異,unnamed namespace 具有 local 的特性)。 這個特性是繼承於 C 裡的
Namespace Aliases有時候 namespace 太長不好去使用,這時候可以用別名: namespace primer = cplusplus_primer;
|
Modern C++ For C Programmers Introduction
此系列以 C 的角度出發看 C++ 具備什麼樣的優勢。預計紀錄下每篇所提到的 C++ 特性,內容為翻閱《C++ Primer》的筆記為主,一篇一留言。
Table Of Contents
The text was updated successfully, but these errors were encountered: