EffectiveC++第四章类和函数:设计与声明学习笔记
条款18: 争取使类的接口完整并且最小
#include <string>
using std::cout;
using std::endl;
namespace sun
{
enum boundscheckingstatus
{no_check_bounds,check_bounds};
class Array
{
public:
Array(int lowbound,int highbound,boundscheckingstatus = no_check_bounds)
{};
};
}; // 名字空间需加上;
int main()
{
using namespace sun;
Array arr1(0,9);
Array arr2(2,18, check_bounds);
system("pause");
return 0;
}
不要忘记友元函数在所有实际应用中都是类的接口的一部分。这意味着友元函数影响着类的接口的完整性和最小性。
条款19: 分清成员函数,非成员函数和友元函数
成员函数和非成员函数最大的区别在于成员函数可以是虚拟的而非成员函数不行。
条款20: 避免public接口出现数据成员
首先,从“一致性”的角度来看这个问题。如果public接口里都是函数,用户每次访问类的成员时就用不着抓脑袋去想:是该用括号还是不该用括号呢?——用括号就是了!因为每个成员都是函数。一生中,这可以避免你多少次抓脑袋啊!
你不买“一致性”的帐?那你总得承认采用函数可以更精确地控制数据成员的访问权这一事实吧?如果使数据成员为public,每个人都可以对它读写;如果用函数来获取或设定它的值,就可以实现禁止访问、只读访问和读写访问等多种控制.
条款21: 尽可能使用const
对指针来说,可以指定指针本身为const,也可以指定指针所指的数据为const,或二者同时指定为const,还有,两者都不指定为const:
char *p = "hello"; // 非const指针,
// 非const数据
const char *p = "hello"; // 非const指针,
// const数据
char * const p = "hello"; // const指针,
// 非const数据
const char * const p = "hello"; // const指针,
// const数据
语法并非看起来那么变化多端。一般来说,你可以在头脑里画一条垂直线穿过指针声明中的星号(*)位置,如果const出现在线的左边,指针指向的数据为常量;如果const出现在线的右边,指针本身为常量;如果const在线的两边都出现,二者都是常量。
条款22: 尽量用“传引用”而不用“传值”
通过引用来传递参数还有另外一个优点:它避免了所谓的“切割问题(slicing problem)”。当一个派生类的对象作为基类对象被传递时,它(派生类对象)的作为派生类所具有的行为特性会被“切割”掉,从而变成了一个简单的基类对象。
#include <string>
using std::cout;
using std::endl;
using std::string;
class window {
public:
string name() const; // 返回窗口名
virtual void display() const // 绘制窗口内容
{ cout << "display window\n" << endl;}
};
class windowwithscrollbars: public window {
public:
virtual void display() const
{ cout << "display windowwithscrollbars\n"; }
};
void print( window w)
{
w.display();
}
//void print(window &w) // 二义性
void print(window *w)
{
(*w).display();
}
int main()
{
windowwithscrollbars w;
print(w); // display window
print( &w ); // display windowwithscrollbars
return 0;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
条款23: 必须返回一个对象时不要试图返回一个引用
据说爱因斯坦曾提出过这样的建议:尽可能地让事情简单,但不要过于简单。
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
条款27: 如果不想使用隐式生成的函数就要显式地禁止它
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////