- 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).
- Building Computer Vision Projects with OpenCV 4 and C++
- Microsoft SQL Server企業級平臺管理實踐
- 數據庫技術與應用教程(Access)
- 輕松學大數據挖掘:算法、場景與數據產品
- 文本數據挖掘:基于R語言
- INSTANT Cytoscape Complex Network Analysis How-to
- 數據庫原理與應用(Oracle版)
- 數據庫原理與設計(第2版)
- 大數據治理與安全:從理論到開源實踐
- 一本書講透Elasticsearch:原理、進階與工程實踐
- 大數據技術原理與應用:概念、存儲、處理、分析與應用
- R Machine Learning Essentials
- The Natural Language Processing Workshop
- Google Cloud Platform for Architects
- Learn Selenium