简体   繁体   中英

Should I use pass-by-const-reference in operator=?

I've always been told that passing a temporary instance into a pass-by-reference-function is not safe, I'm confused about the question below, could you guys explain a bit for me please.

#include <iostream>
using namespace std;
int cnt = 0;
class Complex
{
public:
    //explicit 禁止double隐式转换为Complex
    explicit Complex(double real, double imaginary = 0)
        : real_ (real), imaginary_(imaginary)
    {
        id_ = cnt++;
        cout << "complex constructed id:" << id_ << endl;
    }

    //也可以explicit来禁止隐式调用拷贝构造函数
    Complex(const Complex& other): real_ (other.real_), imaginary_(other.imaginary_)
    {
        id_ = cnt++;
        cout << "complex copy constructed id:" << id_ << endl;
    }

    ~Complex()
    {
        cout << "complex destructed id:" << id_ << endl;
    }

    Complex& operator= (const Complex& rhs)
    {
        cout << "complex operator=" << endl;
        real_ = rhs.real_;
        imaginary_ = rhs.imaginary_;
        return *this;
    }

    //return-by-reference, pass-by-const-reference 
    Complex& operator+= (const Complex& other)
    {
        real_ += other.real_;
        imaginary_ += other.imaginary_;
        return *this;
    }

    //return-by-reference
    Complex& operator++()
    {
        ++real_;
        return *this;
    }

    //return-by-const-value是为了防止a++++的情况
    const Complex operator++(int)
    {
        Complex temp(*this);
    }

    ostream& Print(ostream& os) const
    {
        return os << "(" << real_ << "," << imaginary_ << ")";
    }

private:
    int id_;
    double real_;
    double imaginary_;

};

const Complex operator+ (const Complex& lhs, const Complex& rhs)
{
    cout << "complex operator+ " << endl;
    Complex ret(lhs);
    ret += rhs;
    return ret;
}

ostream& operator<< (ostream& os, const Complex& c)
{
    return c.Print(os);
}

int main()
{
    Complex a(1, 10);
    Complex b(2, 5);
    Complex c(3, 0);
    b = a + c;
    return 0;
}

The code a + c would create a temporary instance of class Complex, which would be passed by const reference into the function operator= . Are there any possibilities that the temporary instance created by a + c is destructed before the operator= is executed, causing the operator= function to fail? I compiled this program with gcc4.4.7 and it printed this:

在此输入图像描述

It seems that the temporary instance was destructed after operator= . I'm still confused about this result, don't know whether this is a result of compiler optimization or the way C++ is. It would be very appreciated if anyone could enlighten me a bit.

This is the way C++ works. Quoting the standard:

A temporary object bound to a reference parameter in a function call persists until the completion of the full-expression containing the call

[12.2/5.1]

So the temporary instance of class Complex created by the expression a + c is guaranteed to be destroyed after the end of the operator= call.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM