本 Wiki 开启了 HTTPS。但由于同 IP 的 Blog 也开启了 HTTPS,因此本站必须要支持 SNI 的浏览器才能浏览。为了兼容一部分浏览器,本站保留了 HTTP 作为兼容。如果您的浏览器支持 SNI,请尽量通过 HTTPS 访问本站,谢谢!
这里会显示出您选择的修订版和当前版本之间的差别。
两侧同时换到之前的修订记录前一修订版后一修订版 | 前一修订版 | ||
cs:programming:cpp:courses:cpp_basic_deep:chpt_2 [2024/04/16 13:05] – [类型别名与指针和引用] codinghare | cs:programming:cpp:courses:cpp_basic_deep:chpt_2 [2024/04/16 15:09] (当前版本) – [对象生命周期] codinghare | ||
---|---|---|---|
行 399: | 行 399: | ||
</ | </ | ||
==decltype== | ==decltype== | ||
- | decltype | + | decltype |
+ | |||
+ | * decltype(val):'' | ||
+ | <code cpp> | ||
+ | //x is an variable name | ||
+ | int x = 3; | ||
+ | //int | ||
+ | decltype(x); | ||
+ | // | ||
+ | // | ||
+ | decltype((x)); | ||
+ | </ | ||
+ | * '' | ||
<code cpp> | <code cpp> | ||
int x = 3; | int x = 3; | ||
行 408: | 行 420: | ||
decltype(y1) y3 = y1; | decltype(y1) y3 = y1; | ||
</ | </ | ||
+ | * '' | ||
+ | <code cpp> | ||
+ | int x = 3; | ||
+ | int* ptr = &x; | ||
+ | //*ptr 是包含了解引用操作符的表达式:此处是 l-value,此处得到的是 int& | ||
+ | decltype(*ptr); | ||
+ | </ | ||
+ | * decltype(auto) [C++14]:用于简化 decltype 的使用 | ||
+ | <code cpp> | ||
+ | // | ||
+ | decltype(3.5+15l) x = 3.5 + 15l; | ||
+ | //C++ 14 的写法 | ||
+ | // 用 auto 来代表繁琐的表达式,并不会导致退化 | ||
+ | // 编译器会将 auto 替换为赋值运算符右边的内容 | ||
+ | decltype(autol) x = 3.5 + 15l; | ||
+ | </ | ||
+ | * concept auto [C++20]:任何 concept 都会包含一系列类型,这些类型的共同特征都可以用 concept 表示。比如 '' | ||
+ | * concept auto 让自动推导受 concept 的范围限制,比如让 auto 只能推断出整数类型的类型: | ||
+ | <code cpp> | ||
+ | #include < | ||
+ | int main() | ||
+ | { | ||
+ | //int | ||
+ | std:: | ||
+ | //error, 3.5 is not an integral | ||
+ | std:: | ||
+ | } | ||
+ | </ | ||
+ | ====域和对象生命周期==== | ||
+ | ===域 Scope=== | ||
+ | 域代表了程序的一部分,域中的 name 有**唯一的含义**: | ||
+ | * 全局域:程序最外部的域,全局对象 | ||
+ | * 块域:大括号限定的域,局部对象 | ||
+ | * 其他类型的域:Namespace, | ||
+ | 域可以进行嵌套,内部域中的 Name 会掩盖外部域中的 Name: | ||
+ | <code cpp> | ||
+ | int x = 3; | ||
+ | int main() | ||
+ | { | ||
+ | int x = 4; | ||
+ | // | ||
+ | | ||
+ | } | ||
+ | </ | ||
+ | ===对象生命周期=== | ||
+ | 生命周期指对象从被**初始化到被销毁**的区间。 | ||
+ | * 全局对象:生命周期为程序的运行期 | ||
+ | * 局部对象:起始于**初始化**,结束于域的执行完成 | ||
+ |