C++的 operator 允许定义新的操作符。
一个种是用于全局的, 一种是属于类的。
对应类的操作符,切记是对类的对象起作用,而非指针 -
比如
class CWinImpl
{
void paint() {}
};
Class CWindow
{
public:
CWindow()
{
m_hWnd = CreateWindow( ...);
m_impl = new CWinImpl;
}
operator HWND() const {return m_hWnd}
CWinImpl* operator->()
{ return m_pImpl; }
private:
HWND m-Hwnd;
CWinImpl *m_impl;
};
void Test()
{
CWindow * win = new CWindow();
HWND h = *win; //OK
//HWND h = win; //ERROR
win->paint() //ERROR
(*win)->paint(); //OK
}
切记切记, 跟驱动打交道久了,C++放下多年了,今天给这个郁闷了一把。