// Example with smart pointers:
template<typename T>class SmartPointer
{
public:
SmartPointer( T* );
operator const void*() const;
};
// Here the designer added conversion to void
// const* to help comparison to the null pointer.
// However a bad side effect to this feature
// is that it is possible to generate equality
// comparison between two smart pointers on
// different types:
void doIt( AA* pa, BB* pb )
{
SmartPointer <aa>
spa( pa );
SmartPointer <bb>
spb( pb );
if (spa == spb) // problem! comparing different pointer types
{}
}
上面的注释和例子已经解释得很清楚了,对operator const void()其实是对类型转换操作符void()的重载。当然也可以写个isValid()之类的函数来实现,但是重载操作符可以提供更简洁、更统一的接口,这样也不用为取个函数名而“苦思冥想”了 ^_^。