跳转至

lec11

约 352 个字 45 张图片 预计阅读时间 1 分钟

1. 运算符重载

1.1 运算符样本

stream extractor && inserter
  • 必须是两个参数的全局函数;
  • image-20221206205927149

  • image-20221206210058528

  • return is是为了链式法则;

  • Creating manipulators:

  • image-20221206211328992
Automatic operator =
  • member wise 和 bit wise 相对;
  • image-20221206211842272
Assignment operator
  • 记得要判断是否是自赋值;
  • image-20221206211936106

  • image-20221206212037760

  • 如果不想赋值不被别人用,可以设置成private或者加=delete;

  • image-20221206212218491
Operator()
  • 不是什么重点;

  • image-20221212190711627

1.2 Value classes(值类)

  • image-20221212190914710

1.3 User-defined type conversions

  • image-20221212191030882

  • 当有单个参数的构造函数时,编译器可以自动地进行隐式类型转换;要是不想隐式类型转换,可以在构造函数前面加 explicit;image-20221212191410209image-20221212191727688

Conversion operations

image-20221212192145071

General form of conversion ops
  • 前面没有return类型,后面没有参数;
  • image-20221212192600589
C++ type conversions
  • image-20221212192739131

  • 以下两者不能同时存在;image-20221212192807305

Do you want to use them?
  • image-20221212193600900
some rules
  • 调用函数时,先找完全一样的,再找系统自动转化,再找用户定义的转化;image-20221212193928115

1.4 Casting operators

image-20221212194153252

static_cast
  • image-20221212194301776

  • image-20221212194549674

dynamic_cast
  • image-20221212194924633
const_cast
  • 把常量指针或者引用便成非常量指针或者应用;image-20221212195006385
reinterpere_cast
  • image-20221212195222526

2. Streams

image-20221212200628634

image-20221212200855526

image-20221212200911527

image-20221212200936724

Manipulator可以改变流的状态,比如控制流的输入输出形式;

image-20221212201123300

cin和cout有缓冲;

cerr与clog的区别,两者都是标准错误流,区别在于cerr不经过缓冲区,直接向显示器输出信息,而clog中的信息存放在缓冲区,缓冲区满或者遇到endl时才输出。

image-20221212201156780

image-20221212201538358

image-20221212201619511

image-20221212201704377

image-20221212201720819

get会在\n之前停下来,而getline是直接吃掉\n。

image-20221212201833691

image-20221212204458543

image-20221212204517562

image-20221212204527736

image-20221212204540204

image-20221212204636161

image-20221212204701634

image-20221212204726447

image-20221212204753914

image-20221212204833581

image-20221212204903584

本文总阅读量