关于operator bool () 和bool operator ==()
operator bool () 提供一个本类型到bool的隐式转换,不允许使用参数。
bool operator ==()可以分为bool operator ==( const bool& other),bool operator ==( const T& other),T代表类型。
即与bool类型的比较,和与本类的比较。
#include <iostream>
using namespace std;
class A
{
public:A(int a) :_a(a) {}operator bool(){cout << "operator bool" << endl;return _a;}private:int _a;};int main(){A a(0);A b(10);if (a)cout << "a" << endl;if(a == b)cout<<"asdasddsa"<<endl; getchar();return 0;}
运行结果如下:
operator bool
operator bool
operator bool
由此可见,在判断if(a == b)时,先把a、b分别转换为bool类型再进行判断。
这一次加上operator ==()
#include <iostream>
using namespace std;
class A
{
public:A(int a) :_a(a) {}operator bool(){cout << "operator bool" << endl;return _a;}bool operator==(const bool &other){cout << "bool operator==(const bool &rhs)" << endl;return (bool)_a == other;}bool operator==(const A &other){cout << " bool operator==(const A&other)" << endl;return this->_a == other._a;}
private: int _a;
};
int main()
{ A a(0);A b(10);A c(10);if (a == true)cout << "a == true" << endl;if (b == c)cout << "b == c" << endl;getchar();return 0;
}
结果如下,
bool operator==(const bool &rhs)
bool operator==(const A&other)
b == c
关于operator bool () 和bool operator ==()相关推荐
- operator new,new operator,placement new的区别
原文地址:http://www.cnblogs.com/jamesmile/archive/2010/04/17/1714311.html,在此感谢 C++中的operator new与new ope ...
- c++中BOOL和bool的区别
From: http://blog.csdn.net/zhongguoren666/article/details/8472805 一:在网上找到了BOOL和bool的区别: 1.类型不同 BOOL为 ...
- c++中TRUE与true的区别,BOOL与bool的区别
FALSE/TRUE与false/true的区别 1.FALSE/TRUE与false/true的区别: false/true是标准C++语言里新增的关键字,而FALSE/TRUE是通过#define ...
- BOOL与bool TRUE与true FALSE与false 区别
FALSE/TRUE与false/true的区别 1.FALSE/TRUE与false/true的区别: false/true是标准C++语言里新增的关键字,而FALSE/TRUE是通过#define ...
- C语言中整型和bool的转换,bool和BOOL类型知识集合
知识点一.C语言中有bool类型吗? 之前一直都没有注意到,最近在用C语言写DSP算法时,偶然间发现我函数中定义的bool类型的变量在VC6.0(我主要用它来检查一下语法错误)中编译居然报错了,说是b ...
- [Windows编程] C++中 bool,BOOL ,VARIANT_BOOL 的区别
接上篇,继续讨论BOOL变量. Windows 中BOOL变量常见的好几种类型:小写的bool,大写的BOOL ,还有VARIANT_BOOL . 小写的bool 的是C++ 标准定义的布尔类型, 各 ...
- BOOL与bool的区别(bool不是c的关键字,c++中bool也不是int)
BOOL 定义 typedef int BOOL; #define FALSE 0 #define TRUE 1 不能完全重载(BOOL的本质就是int),如: //file.h void ...
- iOS中boolean、Boolean、BOOL、bool的区别
bool t1 =1;//true or falseBOOL t2 = 3;//YES or NOBoolean t3 =1; //unsigned char typeboolean_t t4 = 1 ...
- Oracle bool 类型,Bool支持Oracle SQL
Oracle PL / sql支持bool数据类型,而Oracle sql则不支持bool数据类型.当您想要将PL / sql布尔返回值处理回日常sql时(例如下面的例子),这是众所周知的痛苦. 即使 ...
最新文章
- 超越MobileNetV3,谷歌提出MobileDets:移动端目标检测新标杆
- python官网下载步骤64位-windows下载并安装Python的具体步骤
- 网络编程学习笔记(非阻塞读和写)
- 【Python】青少年蓝桥杯_每日一题_2.21_3与质数的判断
- java安全编码指南之:对象构建
- jms activemq_带有ActiveMQ的JMS
- 20180307:python接口测试时json的传参与解析区分
- 自动部署 php,Jenkins实现PHP的自动部署
- Elasticsearch基础(一)mapping
- (课堂画图)毕业论文管理系统
- C++容器(一)——vector
- Odoo 序列和工作流
- vue awe-dnd+ant 自定义表格排序显隐
- 为什么提问能力很重要?
- SIEBEL配置学习笔记
- [UGUI源码剖析]—Rebuild 网格重建(画布刷新)系统
- 金龙云介绍——城轨云相关
- 想做web开发 就学JavaScript
- 【2020年高被引学者】 陶大程 悉尼大学
- 全国计算机技术与软件专业技术资格(水平)考试【软件评测师】-考试内容总结(十二)软件评测基础知识...