Skip to content

Instantly share code, notes, and snippets.

@oscarzhao
Last active July 15, 2016 10:34
Show Gist options
  • Select an option

  • Save oscarzhao/7718b010857721900c4d333276d5a218 to your computer and use it in GitHub Desktop.

Select an option

Save oscarzhao/7718b010857721900c4d333276d5a218 to your computer and use it in GitHub Desktop.
C++面向对象高级编程学习笔记

第一周:设计和实现一个不含指针的class(Geekband)

设计class的注意事项

  1. 数据要封装(尽量private)
  2. 参数和返回值尽量用reference(返回local variable除外)
  3. 能用const一定要用const
  4. 构造函数尽量用initializer_list进行参数初始化

临时对象

临时对象是指在函数作用域块作用域内定义的变量。 临时对象在需要返回一个新对象时,可以有效减少代码的冗余,比如:

# complex.h, derived from ppt
...
省略代码
...

inline complex
operator + (double x, const complex&y) 
{
  return complex(x + real(y), imag(y));
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment