官术网_书友最值得收藏!

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). 

主站蜘蛛池模板: 宣恩县| 洛浦县| 保定市| 深水埗区| 星子县| 周口市| 双江| 五大连池市| 奇台县| 安平县| 洞口县| 西平县| 怀集县| 洛宁县| 新巴尔虎右旗| 万山特区| 宽城| 墨脱县| 贵州省| 资源县| 乌兰察布市| 永康市| 都兰县| 临西县| 新丰县| 长宁区| 炉霍县| 曲阜市| 调兵山市| 诏安县| 固安县| 邓州市| 三原县| 无极县| 泰顺县| 蕉岭县| 潞西市| 集贤县| 饶平县| 兰州市| 库车县|