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

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

主站蜘蛛池模板: 常州市| 襄樊市| 思茅市| 永康市| 句容市| 正镶白旗| 长岛县| 淮北市| 铁岭市| 岱山县| 镇安县| 江津市| 米脂县| 无锡市| 赞皇县| 克东县| 陇南市| 沿河| 新平| 铜陵市| 遵义市| 河西区| 临泽县| 瑞金市| 金溪县| 长泰县| 阳西县| 梅河口市| 都匀市| 延津县| 天镇县| 田阳县| 镇赉县| 东乌珠穆沁旗| 扬中市| 许昌县| 平塘县| 砚山县| 阳朔县| 伊川县| 蚌埠市|