c++类操作符重载
C++ class operator overloading
可以重载类的not操作符:
class TestA
{
public:
bool Test;
const bool operator!(){return !Test;}
};
这样…
TestA A; A.Test = false;
if(!TestA){ //etc }
…可以工作。然而,下面是如何工作的呢?
if(TestA) //Does this have to be overloaded too, or is it the not operator inverted?
我要补充的是,在阅读了它之后,我对所采用的typedef解决方案有点困惑,而且我不完全理解发生了什么,因为它似乎有些模糊。谁能给我解释一下?
你可以写一个operator bool()
。这将强制转换为bool类型,使上述语句成为可能。
您重载operator void*
(像标准库的iostreams)或使用boost的技巧使用"unspecified_bool_type" typedef
(安全bool)。它不会自动反转你的operator!
相关文章:
- 重载操作符+:表达式必须是整型或无作用域枚举类型
- 重载操作符
- 如何重载操作符==外模板类使用友元函数
- 重载*操作符,使其在左右两边都工作
- 重载操作符<对于非随机迭代器
- 在c++中重载操作符的时间和原因
- 如何在c++中重载=操作符来通过引用进行复制
- 如何在c++中获取定义为友元的重载操作符的地址
- 使用重载操作符的文件操作表达式没有给出预期的结果
- 重载操作符()
- 重载操作符()并在类内使用
- 类中的重载操作符+
- 定时使用重载操作符
- c++带类的重载操作符
- 用列表容器重载[]操作符
- 重载操作符=
- 任何重载操作符()的静态检测
- 重载操作符以处理类对象
- 在使用另一个类的类中重载操作符==
- 派生类和基类中的重载操作符不同