zoukankan      html  css  js  c++  java
  • 为什么不要重载 && 和 || 操作符!!!

    理论知识:

    1)&&和 || 是C++中非常特殊的操作符

    2)&&和 || 内置实现了短路规则

    简单说明一下短路规则。

    demo1

    #include <iostream>
    using namespace std;
    
    int main()
    {
    <span style="white-space:pre">	</span>int a1 = 1;
    <span style="white-space:pre">	</span>int a2 = 0;
    <span style="white-space:pre">	</span>if (a1 || a2++) {
    <span style="white-space:pre">		</span>cout << "||
    ";
    <span style="white-space:pre">	</span>}
    
    
    <span style="white-space:pre">	</span>if (a2 && a1++) {
    <span style="white-space:pre">		</span>cout << "&&
    ";
    <span style="white-space:pre">	</span>} // 只输出了|| 。并没有输出&&
    <span style="white-space:pre">	</span>cout << a1; // a1还是等于1;
    <span style="white-space:pre">	</span>return 0;
    }
    第一个if的判断条件中因为a1是真,所以整个逻辑或就是真,不再执行a2++的部分,所以第二个if 语句的判断条件中a2为假,并且导致了整个逻辑与为假,不执行a1++的部分,所以最后的输出a1还是1。这就是内置的短路机制。


    3)操作符重载是靠函数重载来完成的

    4)操作数作为函数参数传递

    5)C++的函数参数都会被求值,无法实现短路规则

    demo2

    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    class Test
    {
    	int i;
    public:
    	Test(int i)
    	{
    		this->i = i;
    	}
    
    	Test operator+ (const Test& obj)
    	{
    		Test ret(0);
    
    		cout << "执行+号重载函数" << endl;
    		ret.i = i + obj.i;
    		return ret;
    	}
    
    	bool operator&& (const Test& obj)
    	{
    		cout << "执行&&重载函数" << endl;
    
    		return i && obj.i;
    	}
    };
    
    // && 从左向右
    int main()
    {
    	int a1 = 0;
    	int a2 = 1;
    
    	cout << "注意:&&操作符的结合顺序是从左向右" << endl;
    
    	if (a1 && (a1 + a2))
    	{
    		cout << "有一个是假,则不在执行下一个表达式的计算" << endl;
    	}
    
    	Test t1 = 0;
    	Test t2 = 1;
    
    	//if( t1 && (t1 + t2)  )
    	//t1  && t1.operator+(t2)
    	// t1.operator&&(  t1.operator+(t2) )   
    
    	// && || 重载他们 不会产生短路效果
    	if ((t1 + t2) && t1)
    	{
    		//t1.operator+(t2) && t1;
    		//(t1.operator+(t2)).operator&&(t1);
    
    		cout << "两个函数都被执行了,而且是先执行了+" << endl;
    	}
    
    	return 0;
    }
    

    说明:比如考虑
    (t1 + t2) && t1

    成员函数的调用本质是

    (t1.operator+(t2)).operator&&(t1);

    这样的结果是不可能产生短路效果的。

    同理:

    t1 && (t1 + t2)
    它的调用本质的是

    t1.operator&&(  t1.operator+(t2) )
    所以我们重载 && 和 || 都不能实现短路效果,所以一般重载的时候我们都不对这两个运算符进行重载。



  • 相关阅读:
    解决Oracle SQL Developer无法连接远程服务器的问题
    [备忘] Automatically reset Windows Update components
    在ASP.NET MVC的Action中直接接受客户端发送过来的HTML内容片段
    Rehosting the Workflow Designer
    解决Onedrive经常无法访问的问题
    最好的简明NodeJS学习材料
    最好的Python简明教程
    在Linux(ubuntu server)上面安装NodeJS的正确姿势
    在Windows中安装NodeJS的正确姿势
    在Windows环境中开始Docker的学习和体验
  • 原文地址:https://www.cnblogs.com/zhangyaoqi/p/4591573.html
Copyright © 2011-2022 走看看