Effective C++
liudy95
这个作者很懒,什么都没留下…
展开
-
Item 11: Handle assignment to self inoperator=
在使用=是,我们可能遇到以下这种自己赋值给自己的情况class Widget{---};Widget w;...w = w;假定我们对=有以下的实现class Bitmap {...};class Widget{...Widget& operator=(const Widget& rhs) { delete pb; pb = new Bitmap(*rhs.pb); return *this;}private: Bitmap *pb;};以上实现在遇原创 2021-06-26 18:02:22 · 142 阅读 · 0 评论 -
Item 10: Have assignment operators return a reference to *this
int x, y, z;x = y = z = 15;在c++中=是右结合(right-associative),所以可以按以上方式进行链式的使用。为了支持以上方式,我们自己所写的数据类型需要按以下方式进行实现class Widget{public: Widget& opoerator=(const Widget& rhs) { ... return *this; } Widget& opoerator+=(const Widget& rhs) {原创 2021-06-26 17:36:53 · 113 阅读 · 0 评论 -
Iterm3: Use const whenever possible
首先,我们介绍下const使用上的语法,如下/* * @Brief: file description * @Author: liudy * @Email: deyin.liu@nscc-gz.cn * @Date: 2021-06-26 16:32:33 * @LastEditors: liudy * @LastEditTime: 2021-06-26 16:41:20 */#include <iostream>using namespace std;int main原创 2021-06-26 17:06:16 · 283 阅读 · 0 评论 -
Item 2:Prefer consts, enums, and inlines to #defines
这个条款我们同样可以理解为 “prefer the compiler to the preprocesor”#define Accelerate_Rate 1.653使用#define不好的地方在于,编译器更本“看不见”符号Accelerate_Rate,因为在预编译期间代码中所有Accelerate_Rate都已经替换成了1.653。这会导致之后代码出现问题Debug的时候,出错信息只会提示1.653,这可能会导致你debug的困扰。所以使用const可能会是更好的选择const double原创 2021-06-26 12:20:21 · 91 阅读 · 0 评论 -
Item 9: Never call virtual functions during construction or destruction
Effective C++Item 9: Never call virtual functions during construction or destructionItem 9: Never call virtual functions during construction or destruction/* * @Brief: Effective c++ item 9 * @Author: liudy * @Email: deyin.liu@nscc-gz.cn * @Date: 202原创 2021-06-24 23:04:27 · 112 阅读 · 0 评论