C++ 重载运算符参数中的&有什么用?

#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;
    }
    // 重载 + 运算符,用于把两个 Box 对象相加
    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;      // 长度
    double breadth;     // 宽度
    double height;      // 高度
};
// 程序的主函数
int main()
{
    Box Box1;                // 声明 Box1,类型为 Box
    Box Box2;                // 声明 Box2,类型为 Box
    Box Box3;                // 声明 Box3,类型为 Box
    double volume = 0.0;     // 把体积存储在该变量中

                             // 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;

    // 把两个对象相加,得到 Box3
    Box3 = Box1 + Box2;

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

    return 0;
}

其中重载运算符函数:

    // 重载 + 运算符,用于把两个 Box 对象相加
    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;
    }

中参数有一个const Box& b,其中的&有什么用,我把他去掉也可以正常运行,这里不是取变量的地址的意思吧?想不通

还有这里的参数只有一个,但是在运算时自身Box也参与了运行
Box3 = Box1 + Box2;
是不是二元运算符有个默认参数是自身,但是可以不写出来,就写其他参数就行
那三个相加:
Box4 = Box1 + Box2 + Box3;
是不是应该这样写:
更新:下面这样写是错误的,已经试过了,+运算符最多一个参数,三个相加的话还是用上面那个写法,直接在外部写三个相加就可以了

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;
}

谢谢

阅读 7.7k
2 个回答

&的用意我只知道除了是引用和位址之外,它还有一个功能就是防止拷贝(比较有效率)。

void swap(int &a, int &b)

void swap(int a, int b)

的差别是一个是整数a b的本身,另一个是整数a b的副本。如果你使用第二个swap函数来进行交换整数的话,你会发现没有换到。因为你所处理的只是在副本上进行交换整数而已,它本身的自己却没有更动到。


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;
    }

知道了&的一些概念后,你可能会问为什么 Box& b前面要放 const!
因为你知道 &会改变对象本身,然后我们只是想用b里面的变数且不想更动到它们,所以前面加const的用意是防止改变对象!

总结来说,const B &b 达到了无副本 + 无法修改的目的。

新手上路,请多包涵

&表示对对象的引用,这里也就是值传递和引用传递的区别。
值传递实际上在栈里会存一份你传入参数的临时副本,所有的操作都在副本上进行(所以不会影响到传入参数)。
而引用传递实际上存储的是的传入参数的地址,并不会生成临时的副本。

如果你的类需要占用很大的内存,值传递的效率会很低(要调用拷贝构造函数)。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题