What is the use of the & in the CPP overload operator argument?

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

where overloaded operator function:

    //  +  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;
    }
The parameter in

has a const Box& b, and what"s the use of &? I can run normally if I remove it. It doesn"t mean to take the address of the variable, is it? I can"t figure it out

and there is only one parameter here, but the Box itself also participates in running
Box3 = Box1 + Box2;
during the operation. Does the binary operator have a default parameter of itself, but you can write other parameters without writing it? add up the
three:
Box4 = Box1 + Box2 + Box3;. Should
be written like this:
Update: it is wrong to write the following. It has been tried. The + operator has at most one parameter. If the three operators add up, you should still use the above method. Just write the three additions on the outside

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

Thank you

CPP c
Feb.27,2021

All I know is that in addition to references and addresses, another function of

& is to prevent copying (more efficient).

void swap (int & a, int & b)

and

void swap (int a, int b)

The difference between

is that one is the integer a b itself, and the other is a copy of the integer a b. If you use the second swap function to swap integers, you will find that you don't get it. Because all you're dealing with is swapping integers on the copy, but it doesn't change itself.

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

now that you know some concepts of &, you may ask why Box& b is preceded by a movie!
because you know & will change the object itself, and then we just want to use the variables in b and don't want to change them, so the intention of adding const before is to prevent changing the object!

to sum up, const B & b achieves the goal of no copy + no modification.


& represents a reference to an object, which is the difference between value passing and reference passing.
value passing actually keeps a temporary copy of your incoming parameters on the stack, and all operations are done on the copy (so it doesn't affect the incoming parameters).
while the reference passes the address of the incoming parameter that is actually stored, it does not make a temporary copy.

if your class takes up a lot of memory, the value transfer efficiency will be very low (to call the copy constructor).

Menu