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

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

主站蜘蛛池模板: 土默特右旗| 南江县| 房产| 开封市| 武义县| 垫江县| 宜兴市| 容城县| 中山市| 宁远县| 宁阳县| 故城县| 鹤壁市| 蛟河市| 乐至县| 鲜城| 怀远县| 什邡市| 噶尔县| 平泉县| 乐安县| 高唐县| 陇南市| 诏安县| 南召县| 青神县| 家居| 鄂尔多斯市| 崇礼县| 文化| 卓尼县| 边坝县| 大理市| 理塘县| 芜湖市| 肇东市| 体育| 长顺县| 若尔盖县| 阳新县| 河曲县|