view c++ as a federation of languages
c
pass by value比pass by reference更高效,c中的引用实质上还是指针
object-oriented c++
有构造函数和析构函数存在,传值会调用拷贝构造函数和析构函数
template c++
pass by reference 更高效,在使用模版时,并不知道是什么类型,使用reference不用拷贝
pass by reference-to-const
STL
prefer const enum inline to define
1 |
|
const 比define更好
1 | class foo{ |
num是类中的专属常量,如果在类外取它的地址的话,需要在类外给出定义式
1 | const int foo::num; |
1 |
|
如果不在类外给出定义式,在类外访问num的地址会出现错误,原因是并没有给num分配地址
1 | class foo { |
也没分配地址,所以要在外面给出定义式,在类中仅仅只是声明式
也可以这样写
1 | class foo{ |
使用inline
替换’#define’