《A Tour of C++ Third Edition》2.User-Defined Types

2.1 Introduction

用户自定义类型主要包括class和枚举,结构体联合体等等就不说了

2.2 Structures

结构体的简单使用示例

2.3 Classes

数据和方法分开有其好处:可以随意使用数据,但随意代表混乱、容易出错,且不希望所有内部数据都裸露,只希望方便地使用,此时需要把数据和方法捆绑在一起

There is no fundamental difference between a struct and a class; a struct is simply a class with
members public by default.

这里也说到结构体和类的区别,仅仅在成员的默认访问权限上有不同

2.4 Enumerations

The class after the enum specifies that an enumeration is strongly typed and that its enumerators
are scoped. Being separate types, enum classes help prevent accidental misuses of constants

enum Color { red, blue, green };
enum Traffic_light { green, yellow, red };
enum AnotherColor { red, blue, green };报错重定义
// enum class 可以解决这个问题,限定了作用域
Color col = Color::red;
Traffic_light light = Traffic_light::red;
enum class Color { red, blue, green };
enum class Traffic_light { green, yellow, red };
Color col = Color::red;
Traffic_light light = Traffic_light::red;

整数可以初始化枚举,但枚举不能初始化整数除非强制转换

int i = Color::red; // error: Color::red is not an int
Color c = 2; // initialization error: 2 is not a Color

Color x = Color{5}; // OK, but verbose
Color y {6}; // also OK

int x = int(Color::red);

枚举默认只支持初始化、赋值、==、<这些操作,但我们可以自定义补充:

enum class Color { red, blue, green };
enum class Traffic_light { green, yellow, red };

Color col = Color::red;
Traffic_light light = Traffic_light::red;
Traffic_light& operator++(Traffic_light& t) // prefix increment: ++
{
	using enum Traffic_light; // here, we are using Traffic_light
	switch (t) {
		case green: return t = yellow;
		case yellow: return t = red;
		case red: return t = green;
	}
}
int main() {
	enum Color { red, green, blue };//C中枚举,内部作用域和枚举类型相同
	int col = green;//使用不需要加Color::

}

2.5 Unions

enum class Type { ptr, num }; // a Type can hold values ptr and num (§2.4)
struct Entry {
	string name; // string is a standard-library type
	Type t;
	double* p; // use p if t==Type::ptr
	int i; // use i if t==Type::num
};
void f(Entry* pe) {
	if (pe->t == Type::num)
		cout << pe->i;
	// ...
}

可以看到p和i两块内存是永远无法同时使用的,不如写成联合体:

union Value {
	double* p;
	int i;
};
struct Entry {
	string name;
	Type t;
	Value v; // use v.p if t==Type::ptr; use v.i if t==Type::num
};
void f(Entry* pe) {
	if (pe->t == Type::num)
		cout << pe->v.i;
	// ...
}

标准库使用variant类型替代了联合体:


struct Entry {
	string name;
	variant<double*, int> v;
};
void f(Entry* pe) {
	if (holds_alternative<int>(pe->v)) // does *pe hold an int? (see §15.4.1)
		cout << get<int>(pe->v); // get the int
	// ...
}

std::variant - cppreference.com

  • 9
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
This book is devoted to practical C programming. C is currently the premier language for software developers. That's because it's widely distributed and standard. Newer languages are available, such as C++, but these are still evolving. C is still the language of choice for robust, portable programming. This book emphasizes the skills you will need to do real-world programming. It teaches you not only the mechanics of the C language, but the entire life cycle of a C program as well (including the program's conception, design, code, methods, debugging, release, documentation, maintenance, and revision). Good style is emphasized. To create a good program you must do more than just type in code. It is an art in which writing and programming skills blend themselves together to form a masterpiece. True art can be created. A well-written program not only functions correctly, but is simple and easy to understand. Comments allow the programmer to include descriptive text inside the program. When clearly written, a commented program is highly prized. A program should be as simple as possible. A programmer should avoid clever tricks. This book stresses simple, practical rules. For example, there are 15 operator precedence rules in C. These can be simplified into two rules: 1. Multiply and divide come before add and subtract. 2. Put parentheses around everything else. Consider two programs. One was written by a clever programmer using all the tricks. The program contains no comments, but it works. The other program is well commented and nicely structured, but it doesn't work. Which program is more useful? In the long run, the broken one. It can be fixed. Although the clever program works now, sooner or later all programs have to be modified. The worst thing that you will ever have to do is to modify a cleverly written program. This handbook is written for people with no previous programming experience or programmers who already know C and want to improve their style and reliability. You should have access to a computer and TEAM FLY PRESENTS 9 know how to use the basic functions such as a text editor and the filesystem. Specific instructions are given for producing and running programs using the UNIX operating system with a generic cc compiler or the Free Software Foundation's gcc compiler. For MS-DOS/Windows users, instructions are included for Borland C++, Turbo C++, and Microsoft Visual C++. (These compilers compile both C and C++ code.) The book also gives examples of using the programming utility make for automated program production. How This Book is Organized You must crawl before you walk. In Part I we teach you how to crawl. These chapters enable you to write very simple programs. We start with the mechanics of programming and programming style. Next, you learn how to use variables and very simple decision and control statements. In Chapter 7, we take you on a complete tour of the software life cycle to show you how real programs are created. Part II describes all of the other simple statements and operators that are used in programming. You'll also learn how to organize these statements into simple functions. In Part III we take our basic declarations and statements and learn how they can be used in the construction of advanced types such as structures, unions, and classes. We'll also introduce the concept of pointers. Finally, a number of miscellaneous features are described Part IV. Chapter

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

+xiaowenhao+

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值