- 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).
- 數(shù)據(jù)庫原理及應(yīng)用教程(第4版)(微課版)
- Unity 5.x Game AI Programming Cookbook
- Test-Driven Development with Mockito
- Libgdx Cross/platform Game Development Cookbook
- Learning JavaScriptMVC
- 數(shù)據(jù)庫系統(tǒng)原理及應(yīng)用教程(第4版)
- 城市計算
- 軟件成本度量國家標(biāo)準(zhǔn)實施指南:理論、方法與實踐
- Python醫(yī)學(xué)數(shù)據(jù)分析入門
- The Game Jam Survival Guide
- MySQL 8.x從入門到精通(視頻教學(xué)版)
- 企業(yè)級容器云架構(gòu)開發(fā)指南
- 智慧的云計算
- 數(shù)據(jù)分析師養(yǎng)成寶典
- Expert Python Programming(Third Edition)