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

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

主站蜘蛛池模板: 郓城县| 棋牌| 长子县| 合肥市| 汾阳市| 义马市| 泽普县| 维西| 合江县| 大余县| 阿拉善右旗| 双柏县| 临沂市| 商都县| 永福县| 兴宁市| 涿州市| 芷江| 怀化市| 吐鲁番市| 棋牌| 富锦市| 搜索| 双桥区| 武宣县| 闽清县| 迭部县| 舒兰市| 罗田县| 邢台市| 太仆寺旗| 吉首市| 石屏县| 大名县| 乾安县| 香格里拉县| 柳江县| 茌平县| 东乌| 普安县| 元谋县|