在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問(wèn)答/C  C++/ C++ 重載運(yùn)算符參數(shù)中的&有什么用?

C++ 重載運(yùn)算符參數(shù)中的&有什么用?

#include <iostream>
using namespace std;

class Box
{
public:

    double getVolume(void)
    {
        return length * breadth * height;
    }
    void setLength(double len)
    {
        length = len;
    }

    void setBreadth(double bre)
    {
        breadth = bre;
    }

    void setHeight(double hei)
    {
        height = hei;
    }
    // 重載 + 運(yùn)算符,用于把兩個(gè) Box 對(duì)象相加
    Box operator+(const Box& b)
    {
        Box box;
        box.length = this->length + b.length;
        box.breadth = this->breadth + b.breadth;
        box.height = this->height + b.height;
        return box;
    }
private:
    double length;      // 長(zhǎng)度
    double breadth;     // 寬度
    double height;      // 高度
};
// 程序的主函數(shù)
int main()
{
    Box Box1;                // 聲明 Box1,類型為 Box
    Box Box2;                // 聲明 Box2,類型為 Box
    Box Box3;                // 聲明 Box3,類型為 Box
    double volume = 0.0;     // 把體積存儲(chǔ)在該變量中

                             // Box1 詳述
    Box1.setLength(6.0);
    Box1.setBreadth(7.0);
    Box1.setHeight(5.0);

    // Box2 詳述
    Box2.setLength(12.0);
    Box2.setBreadth(13.0);
    Box2.setHeight(10.0);

    // Box1 的體積
    volume = Box1.getVolume();
    cout << "Volume of Box1 : " << volume << endl;

    // Box2 的體積
    volume = Box2.getVolume();
    cout << "Volume of Box2 : " << volume << endl;

    // 把兩個(gè)對(duì)象相加,得到 Box3
    Box3 = Box1 + Box2;

    // Box3 的體積
    volume = Box3.getVolume();
    cout << "Volume of Box3 : " << volume << endl;

    return 0;
}

其中重載運(yùn)算符函數(shù):

    // 重載 + 運(yùn)算符,用于把兩個(gè) Box 對(duì)象相加
    Box operator+(const Box& b)
    {
        Box box;
        box.length = this->length + b.length;
        box.breadth = this->breadth + b.breadth;
        box.height = this->height + b.height;
        return box;
    }

中參數(shù)有一個(gè)const Box& b,其中的&有什么用,我把他去掉也可以正常運(yùn)行,這里不是取變量的地址的意思吧?想不通

還有這里的參數(shù)只有一個(gè),但是在運(yùn)算時(shí)自身Box也參與了運(yùn)行
Box3 = Box1 + Box2;
是不是二元運(yùn)算符有個(gè)默認(rèn)參數(shù)是自身,但是可以不寫出來(lái),就寫其他參數(shù)就行
那三個(gè)相加:
Box4 = Box1 + Box2 + Box3;
是不是應(yīng)該這樣寫:
更新:下面這樣寫是錯(cuò)誤的,已經(jīng)試過(guò)了,+運(yùn)算符最多一個(gè)參數(shù),三個(gè)相加的話還是用上面那個(gè)寫法,直接在外部寫三個(gè)相加就可以了

Box operator+(const Box& b,const Box& c)
{
    Box box;
    box.length = this->length + b.length + c.length;
    box.breadth = this->breadth + b.breadth + c.breadth;
    box.height = this->height + b.height + c.height ;
    return box;
}

謝謝

回答
編輯回答
淚染裳

&的用意我只知道除了是引用和位址之外,它還有一個(gè)功能就是防止拷貝(比較有效率)。

void swap(int &a, int &b)

void swap(int a, int b)

的差別是一個(gè)是整數(shù)a b的本身,另一個(gè)是整數(shù)a b的副本。如果你使用第二個(gè)swap函數(shù)來(lái)進(jìn)行交換整數(shù)的話,你會(huì)發(fā)現(xiàn)沒(méi)有換到。因?yàn)槟闼幚淼闹皇窃诟北旧线M(jìn)行交換整數(shù)而已,它本身的自己卻沒(méi)有更動(dòng)到。


Box operator+(const Box& b)
    {
        Box box;
        box.length = this->length + b.length;
        box.breadth = this->breadth + b.breadth;
        box.height = this->height + b.height;
        return box;
    }

知道了&的一些概念后,你可能會(huì)問(wèn)為什么 Box& b前面要放 const!
因?yàn)槟阒?&會(huì)改變對(duì)象本身,然后我們只是想用b里面的變數(shù)且不想更動(dòng)到它們,所以前面加const的用意是防止改變對(duì)象!

總結(jié)來(lái)說(shuō),const B &b 達(dá)到了無(wú)副本 + 無(wú)法修改的目的。

2017年8月30日 12:21
編輯回答
浪婳

&表示對(duì)對(duì)象的引用,這里也就是值傳遞和引用傳遞的區(qū)別。
值傳遞實(shí)際上在棧里會(huì)存一份你傳入?yún)?shù)的臨時(shí)副本,所有的操作都在副本上進(jìn)行(所以不會(huì)影響到傳入?yún)?shù))。
而引用傳遞實(shí)際上存儲(chǔ)的是的傳入?yún)?shù)的地址,并不會(huì)生成臨時(shí)的副本。

如果你的類需要占用很大的內(nèi)存,值傳遞的效率會(huì)很低(要調(diào)用拷貝構(gòu)造函數(shù))。

2017年3月23日 05:19