C++初学者可能不知道句柄类为何物。其实说白了,句柄类就是为了消除来自同一个基类的派生类的操作差异性而建立的一个中间层,它管理了这些派生类的指针,消除了各种派生类建立和使用的不一致性。
我为了建立句柄类,首先要找一个适合并且容易用OOP实现的东西,后来就想到了GUI,于是我就设计了一个按钮的类族,建立了三个简单的类,分别为Button类,CommandButton类,ImageButton类。Button类是所有按钮类的基类,而CommandButton和ImageButton都继承自Button类。这些类的关系如下图所示:
Button
|
- CommandButton
- ImageButton
其中Button类的接口和实现在 button.h 中。
CommandButton类的接口和实现在 cmdbtn.h 中。
ImageButton类的接口和实现在 imgbtn.h 中。
然后呢,我就设计了一个叫做ButtonHandle的类,顾名思义,也就是Button的句柄类了。
ButtonHandle的接口在 btnhandle.h 中,实现在 btnhandle.cpp 中。
程序使用 g++ 4 在 Slackware Linux 13 下编译通过,还使用 MinGW g++ 3.4.5 在 Windows 7 下编译通过。
废话不说了,直接贴代码。
button.h
cmdbtn.h
imgbtn.h
btnhandle.h
main.cpp(测试程序)
btnhandle.cpp