C++重载符号
来源:互联网 发布:nemo阿拉伯语软件 编辑:程序博客网 时间:2024/06/05 08:30
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) ;// 工作正常
- C符号重载
- C语言中的符号重载
- C语言中的符号重载
- C语言中的符号重载
- C语言中的符号重载
- C语言中的符号重载
- C语言中的符号重载
- C语言拾遗(二):符号重载
- C语言中的符号重载,static
- []符号重载
- 符号重载
- C语言中的符号重载(Expert C Programming笔记)
- C语言里的符号重载《C专家编程》
- C#符号重载
- 重载了>等符号:
- (转)运算符号重载
- 符号重载的理解
- C++重载符号
- C#构告函数的继承问题
- zz: 如何改变PDF文档的背景颜色
- WIN7以上IIS局域网访问
- std::map排序的原理
- 50点提高PHP编程效率
- C++重载符号
- winform获取路径
- Qt Linux下界面自动化测试(续)之跨语言调用tdriver
- 创建XML文档并保存到磁盘文件中(UTF-8)
- sql 修改表结构 视图如何更新
- 图片上传插件jquery.uploadify
- WINDOWS下线程同步探讨
- WordLocalView
- 男女之间的友情和爱情