- Hands-On System Programming with C++
- Dr. Rian Quinn
- 165字
- 2021-07-02 14:42:34
Scope
One major difference between C and C++ is how the construction and destruction of an object is handled. Let's take the following example:
#include <iostream>
struct mystruct {
int data1{42};
int data2{42};
};
int main(void)
{
mystruct s;
std::cout << s.data1 << '\n';
}
// > g++ scratchpad.cpp; ./a.out
// 42
Unlike in C, in C++ we are able to use the {} operator to define how we would like the data values of the structure to be initialized. This is possible because, in C++, objects (both structures and classes) contain constructors and destructors that define how the object is initialized on construction and destroyed on destruction.
When system programming, this scheme will be used extensively, and the idea of the construction and destruction of objects will be leveraged throughout this book when handling system resources. Specifically, a scope will be leveraged to define the lifetime of an object, and thus the system resource that the object owns, using a concept called Resource Acquisition is Initialization (RAII).
- 數據庫基礎教程(SQL Server平臺)
- Google Visualization API Essentials
- App+軟件+游戲+網站界面設計教程
- Libgdx Cross/platform Game Development Cookbook
- Python數據分析:基于Plotly的動態可視化繪圖
- 達夢數據庫性能優化
- HikariCP連接池實戰
- 數據庫應用系統開發實例
- Web Services Testing with soapUI
- Oracle 11g+ASP.NET數據庫系統開發案例教程
- 數據庫原理與設計實驗教程(MySQL版)
- 一類智能優化算法的改進及應用研究
- 數據迷霧:洞察數據的價值與內涵
- SQL進階教程(第2版)
- Configuration Management with Chef-Solo