《Effective Modern C++》第3章学习记录2

Item10: Prefer scoped enums to unscoped enums

As a general rule, declaring a name inside curly braces limits the visibility of that name to the scope defined by the braces.

  • C++98-style enums: unscoped
  • C++11 scoped enums
  • enum classes

Scoped enums have a second compelling advantage: their enumerators are much more strongly typed.

  • forward-declared

enum class color

To make efficient use of memory, compilers often want to choose the smallest underlying type for an enum that’s sufficient to represent its range of enumerator values.

  • use of fwd-declared enum

The header containing these declarations requires no recompilation if enum’s definition is revised

Item 11: Prefer deleted functions to private undefined ones

  • copy constructor
  • copy assignment operator

The C++98 approach to preventing use of these functions is to declare them private and not define them

  • “= delete”
  • delete functions

Deleted functions may not be used in any way, so even code that’s in member and friend functions will fail to compile if it tries to copy objects.

Things to remember

  • Prefer deleted functions to private udefined ones
  • Any function may be deleted, including non-member functions and template instantiations

Item 12: Declare overriding functions override

The world of object-oriented programming in C++ revolves around classes, inheritance, and virtual functions.

Among the most fundamental ideas in this world is that virtual function implementations in derived classes override the implementations of their base class counterparts.

Member function reference qualifers are one of C++11’s less-public features

  • declare it override

  • two contextual keywords

override, final

Things to Remember

  • Declare overriding functions override
  • Member function reference qualifiers make it possible to treat lvalue and rvalue objects (*this) differently
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值