mirror of
https://github.com/Light-City/CPlusPlusThings.git
synced 2026-10-28 19:04:45 +08:00
- const README: rewrite the space-saving claim per issue #5 follow-ups (constant-expression const vs #define immediates; which const qualify as constant expressions) - const README: fix the class-section diagnosis per issue #338 (overload resolution picks the const overload; the restriction only surfaces once that overload is removed) - sizeof README: virtual-inheritance result is compiler-dependent (GCC merges vbptr+vfptr -> 16, MSVC keeps them separate -> 32) #246 - static README: new section on internal linkage / access restriction with anonymous-namespace comparison #142 - 10-day practice: full problem list table + fixed day links #136 - new design_pattern/singleton/singleton_default.hpp: reusable thread-safe CRTP singleton template, compile+concurrency tested #303
This commit is contained in:
@@ -1,9 +1,12 @@
|
||||
# using那些事
|
||||
> 本文档由 [C++那些事网站](https://light-city.github.io/stories_things/cpp-things/using/) 同步生成,网站版本含多语言与更新排版。
|
||||
|
||||
# C++ using 声明与别名
|
||||
|
||||
## 关于作者:
|
||||
|
||||
个人公众号:
|
||||
|
||||

|
||||

|
||||
|
||||
## 基本使用
|
||||
|
||||
@@ -48,7 +51,7 @@ int main()
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
完整代码见:[using_global.cpp](using_global.cpp)
|
||||
完整代码见:[using_global.cpp](https://github.com/Light-City/CPlusPlusThings/tree/master/basic_content/using/using_global.cpp)
|
||||
## 改变访问性
|
||||
|
||||
```
|
||||
@@ -67,7 +70,7 @@ protected:
|
||||
```
|
||||
|
||||
类Derived私有继承了Base,对于它来说成员变量n和成员函数size都是私有的,如果使用了using语句,可以改变他们的可访问性,如上述例子中,size可以按public的权限访问,n可以按protected的权限访问。
|
||||
完整代码见:[derived_base.cpp](derived_base.cpp)
|
||||
完整代码见:[derived_base.cpp](https://github.com/Light-City/CPlusPlusThings/tree/master/basic_content/using/derived_base.cpp)
|
||||
## 函数重载
|
||||
|
||||
在继承过程中,派生类可以覆盖重载函数的0个或多个实例,一旦定义了一个重载版本,那么其他的重载版本都会变为不可见。
|
||||
@@ -106,7 +109,7 @@ int main()
|
||||
```
|
||||
如上代码中,在派生类中使用using声明语句指定一个名字而不指定形参列表,所以一条基类成员函数的using声明语句就可以把该函数的所有重载实例添加到派生类的作用域中。此时,派生类只需要定义其特有的函数就行了,而无需为继承而来的其他函数重新定义。
|
||||
|
||||
完整代码见:[using_derived.cpp](using_derived.cpp)
|
||||
完整代码见:[using_derived.cpp](https://github.com/Light-City/CPlusPlusThings/tree/master/basic_content/using/using_derived.cpp)
|
||||
## 取代typedef
|
||||
|
||||
C中常用typedef A B这样的语法,将B定义为A类型,也就是给A类型一个别名B
|
||||
@@ -117,4 +120,4 @@ C中常用typedef A B这样的语法,将B定义为A类型,也就是给A类
|
||||
typedef vector<int> V1;
|
||||
using V2 = vector<int>;
|
||||
```
|
||||
完整代码见:[using_typedef.cpp](using_typedef.cpp)
|
||||
完整代码见:[using_typedef.cpp](https://github.com/Light-City/CPlusPlusThings/tree/master/basic_content/using/using_typedef.cpp)
|
||||
Reference in New Issue
Block a user