1、Never Return a Reference to a Local Object
There's one crucially important thing to understand about returning a
reference: Never return a reference to a local variable.
Eg :
// Disaster: Function returns a reference to a local object
const string &manip(const string& s)
{
string ret = s;
// transform ret in some way
return ret; // Wrong: Returning reference to a local object!
}
2、Put inline Functions in Header Files
Unlike other function definitions, inlines should be defined in header files.
Whenever an inline function is added to or changed in a header file, every source
file that uses that header must be recompiled
3、在类成员里定义函数默认会作为 inline 函数
A member function that is defined inside the class is implicitly treated as an inline function
4、const成员函数 const必须放在函数末尾,且声明和定义必须完全一致
const成员函数不能修改任何成员变量值
const成员变量也只能调用const成员函数
5、函数重载
Functions cannot be overloaded based only on differences in the return type
重载决策是一种编译时机制,用于在给定了参数列表和一组候选函数成员的情况下,选择一个
最佳函数成员来实施调用。函数重载就是一个类中有几个同名函数但参数表不同
也就是说,函数是否重载应该是在一个Scope,即一个域内,有相同名字、但参数不同的的函数
他们只从参数不同来区分,不靠返回类型辨别
Eg : int add(int a)
double add(int a) 这两个不是函数重载,会再编译时出错认为为同一个函数
特殊情况:当参数是Const指针时,是重载
void f( int *)
void f( const int *) 这两个是重载
但是,void f( int )
void f( const int ) 这两个不是重载
6、函数指针 Pointers to Functions
A function pointer is just thata pointer that denotes a function rather than an object.
Like any other pointer, a function pointer points to a particular type. A function's type
is determined by its return type and its parameter list. A function's name
is not part of its type:
// pf points to function returning bool that takes two const string references
bool (*pf)(const string &, const string &);
This statement declares pf to be a pointer to a function that takes two const
string& parameters and has a return type of bool.
使用TypeDef 可以简化函数指针定义
Using Typedefs to Simplify Function Pointer Definitions
typedef bool (*cmpFcn)(const string &, const string &);
7、函数指针参数(一般回调函数使用)
/* useBigger function's third parameter is a pointer to function
* that function returns a bool and takes two const string references
* two ways to specify that parameter:
*/
// third parameter is a function type and is automatically treated as a pointer to function
void useBigger(const string &, const string &,
bool(const string &, const string &));
// equivalent declaration: explicitly define the parameter as a pointer to function
void useBigger(const string &, const string &,
bool (*)(const string &, const string &));
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
第八章 IO
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
第十二章 IO
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
1、构造函数
对于成员变量为 const, (reference )&,或者没有默认构造函数的类 类型,必须使用构造函数参数表形式初始化
class ConstRef {
public:
ConstRef(int ii);
private:
int i;
const int ci;
int &ri;
};
// ok: explicitly initialize reference and const members
ConstRef::ConstRef(int ii): i(ii), ci(i), ri(ii) { }
We must use an initializer for any const or reference member or for any member of a
class type that does not have a default constructor.
注:最好使成员函数初始化的顺序与定义的顺序一致
It is a good idea to write constructor initializers in the same order as the members
are declared. Moreover, when possible, avoid using members to initialize other members.
2、使用explicit阻止构造函数默认转换
When a constructor is declared explicit, the compiler will not use it as a conversion operator
explicit Sales_item(const std::string &book = ""):
isbn(book), units_sold(0), revenue(0.0) { }
explicit Sales_item(std::istream &is);
2009-11-24
最新推荐文章于 2024-06-07 22:24:10 发布