1.operator()
这个符号的意思是调用一个对象的()调用,请看下例:
struct v_cpr
{ // functor for operator<
bool operator()(const IPValueRange& _Left, const IPValueRange& _Right) const
{ // apply operator< to operands
if( FLAG_COMPARED == _Left.nFlag && FLAG_COMPARED == _Right.nFlag)
{
if( !(_Left.nMaxIPValue < _Right.nMinIPValue) && _Left.nMinIPValue < _Right.nMinIPValue)
{
return false;
}
}
return _Left.nMinIPValue < _Right.nMinIPValue && _Left.nMaxIPValue < _Right.nMinIPValue;
}
};
typedef map<IPValueRange, string, v_cpr> IPValueMapInfo;
// 其中的比较发生在 v_cpr(IPValueRange_Left, IPValueRange_Right),然后返回一个结果值。
2.operator->
T* operator ->() const
{
return 指针 ;
}
3.operator *()
T& operator*() const
{
return 指针对象 ;
}
4.operator void*() ; 表示转化为void* 指针。
例如:
template<class T>
class SmartPtr
{
public:
...
operator void*() ;// 该函数没有返回值,returns 0 if the smart ptr is null,nonzero otherwise
...
}
SmartPtr<TreeNode> ptn ;
...
if (ptn == 0)...// OK
if (ptn)...// OK
if(!pth)...// OK
This is similar to a conversion provided by the iostream classes,and it explains why it's possible to write code like this:
ifstream inputFile("datafile.dat") ;
if(inputFile)// OK
5.operator!
示例:
template<class T>
class SmartPtr
{
public:
...
bool operator!() const // returns true if and only if the smart ptr is null
...
} ;
This lets your clients program like this.
SmartPtr<TreeNode> ptn ;
...
if(!ptn)
{
...
}
else
{
...
}
6.operator T*
template<class T>
class DBPtr
{
public:
...
operator T*() { return pointee ;}
...
} ;
DBPtr<Tuple> pt ;
normalize(Tuple* pt) ;// 工作正常