溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

C++邏輯操作符怎么使用

發(fā)布時(shí)間:2022-06-01 10:59:50 來(lái)源:億速云 閱讀:144 作者:iii 欄目:開(kāi)發(fā)技術(shù)

本篇內(nèi)容主要講解“C++邏輯操作符怎么使用”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“C++邏輯操作符怎么使用”吧!

一、邏輯運(yùn)算符的原生語(yǔ)義

  1. 操作數(shù)只有兩種值( true和 false )邏

  2. 輯表達(dá)式不用完全計(jì)算就能確定最終值

  3. 最終結(jié)果只能是 true 或者 false

下面看一個(gè)邏輯表達(dá)式的代碼:

#include <iostream>
#include <string>
using namespace std;
int func(int i)
{
    cout << "int func(int i): i = " << i << endl;
    return i;
}
int main()
{
    if (func(0) && func(1))
    {
        cout << "Result is true!" << endl;
    }
    else 
    {
        cout << "Result is False!" << endl;
    }
    cout << endl;
    if (func(0) || func(1))
    {
        cout << "Result is true!" << endl;
    }
    else 
    {
        cout << "Result is False!" << endl;
    } 
    return 0;
}

輸出結(jié)果如下:

C++邏輯操作符怎么使用

這就是邏輯操作符的短路規(guī)則,可以參照我之前寫(xiě)的詳細(xì)講解邏輯運(yùn)算符的使用

二、重載邏輯操作符

邏輯操作符可以重載嗎?重載邏輯操作符有什么意義?

下面看一個(gè)重載邏輯操作符示例:

#include <iostream>
using namespace std;
class Test
{
    int mValue;
public:
    Test(int v)
    {
        mValue = v;
    }
    int value() const
    {
        return mValue;
    }
};
bool operator &&(const Test& l, const Test& r)
{
    return l.value() && r.value();
}
bool operator ||(const Test& l, const Test& r)
{
    return l.value() || r.value();
}
Test func(Test i)
{
    cout << "Test func(Test i): i.value() = " << i.value() << endl;
    return i;
}
int main()
{
    Test t0(0);
    Test t1(1);
    if (func(t0) && func(t1))
    {
        cout << "Result is true!" << endl;
    }
    else
    {
        cout << "Result is false!" << endl;
    }
    cout << endl;
    if (func(t0) || func(t1))
    {
        cout << "Result is true!" << endl;
    }
    else
    {
        cout << "Result is false!" << endl;
    }
}

輸出結(jié)果如下:

C++邏輯操作符怎么使用

按照短路法則,func(t0) && func(t1) 應(yīng)該只執(zhí)行 func(t0),這里卻輸出了func(t0) 和 func(t1) 運(yùn)行后的值,這是為什么呢?且看下面解析。

問(wèn)題的本質(zhì)分析

  1. C++ 通過(guò)函數(shù)調(diào)用擴(kuò)展操作符的功能

  2. 進(jìn)入函數(shù)體前必須完成所有參數(shù)的計(jì)算

  3. 函數(shù)參數(shù)的計(jì)算次序是不定的

  4. 短路法則完全失效

邏輯操作符重載后無(wú)法完全實(shí)現(xiàn)原生的語(yǔ)義。

上述代碼等效寫(xiě)法如下:

#include <iostream>
using namespace std;
class Test
{
    int mValue;
public:
    Test(int v)
    {
        mValue = v;
    }
    int value() const
    {
        return mValue;
    }
};
bool operator &&(const Test& l, const Test& r)
{
    return l.value() && r.value();
}
bool operator ||(const Test& l, const Test& r)
{
    return l.value() || r.value();
}
Test func(Test i)
{
    cout << "Test func(Test i): i.value() = " << i.value() << endl;
    return i;
}
int main()
{
    Test t0(0);
    Test t1(1);
    if (operator && (func(t0), func(t1)))
    {
        cout << "Result is true!" << endl;
    }
    else
    {
        cout << "Result is false!" << endl;
    }
    cout << endl;
    if (operator || (func(t0), func(t1)))
    {
        cout << "Result is true!" << endl;
    }
    else
    {
        cout << "Result is false!" << endl;
    }
}

輸出結(jié)果和上面一樣:

C++邏輯操作符怎么使用

將func(t0) && func(t1) 改寫(xiě)成operator && (func(t0), func(t1)),就不難理解為什么了。核心就兩點(diǎn):

1.進(jìn)入函數(shù)體前必須完成所有參數(shù)的計(jì)算

2.函數(shù)參數(shù)的計(jì)算次序是不定的

一些有用的建議

  • 實(shí)際工程開(kāi)發(fā)中避免重載邏輯操作符

  • 通過(guò)重載比較操作符代替邏輯操作符重載

  • 直接使用成員函數(shù)代替邏輯操作符重載

  • 使用全局函數(shù)對(duì)邏輯操作符進(jìn)行重載

到此,相信大家對(duì)“C++邏輯操作符怎么使用”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢(xún),關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

c++
AI