4

I am confused about returning the const reference in C++. So i write below code block and test on gnu c++ and visual studio. And find different answer. Could anyone tell the benefit using return const reference in C++ and the reason cause different behavior on differnt compiler.

#include <iostream>
using namespace std;

class A
{
public:
    A(int num1, int num2):m_num1(num1), m_num2(num2)
    {
            cout<<"A::A"<<endl;
    }
    const A& operator * (const A & rhs) const
    {
            return A(this->m_num1 * rhs.m_num1, this->m_num2*rhs.m_num1);
    }

    A(const A& rhs)
{
            this->m_num1 = rhs.m_num1;
            this->m_num2 = rhs.m_num2;
            cout<<"A::A(A&)"<<endl;
    }
    const A& operator = (const A& rhs)
    {
            cout<<"A::Operator="<<endl;
            return *this;
    }
    void Display();
private:
    int m_num1;
    int m_num2;
};

void A::Display()
{
    cout<<"num1:"<<m_num1<<" num2:"<<m_num2<<endl;
}

int main()
{
    A a1(2,3), a2(3,4);
    A a3 = a1 * a2;
    a3.Display();
    return 0;
}

On Gnu C++, it did report the correct answer. But failed on visual studion compiler.

Roger Luo
  • 355
  • 1
  • 2
  • 10

3 Answers3

8

This is returning a reference to a local variable, which is not allowed:

const A& operator * (const A & rhs) const
{
    return A(this->m_num1 * rhs.m_num1, this->m_num2*rhs.m_num1);
}

You have a dangling reference and undefined behaviour.

Related

Community
  • 1
  • 1
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
1
 const A& operator * (const A & rhs) const
 {
     return A(this->m_num1 * rhs.m_num1, this->m_num2*rhs.m_num1);
 }

This is bad here.

You are returning a dangling reference, if it works it is coincidence.

You are returning something that exists in a stack frame which has been destroyed. Just return by value to fix it.

Robᵩ
  • 163,533
  • 20
  • 239
  • 308
111111
  • 15,686
  • 6
  • 47
  • 62
1
const A& operator * (const A & rhs) const
{
    return A(this->m_num1 * rhs.m_num1, this->m_num2*rhs.m_num1);
}

this function you return a reference to a local variable. Local variable will destroyed when return from this funcutin operator* . So it is dangerous. You can just return A, but not A&.When return A, function will copy a temp A for return.

shihongzhi
  • 1,921
  • 16
  • 17
  • "When return A, function will copy a temp A for return.": It actually will store the temp in the space allocated for the return value. No copy will arise. It is one of the "mandatory optimizations" that came with C++11 – Emilio Garavaglia Mar 13 '12 at 15:02
  • @EmilioGaravaglia: That only applies if returning by value. Here we are returning by reference. Anyway not all compilers support C++11 yet. – Martin York Mar 13 '12 at 15:40
  • @LokiAstari: I'm not referring to the code in the post, but to the particular phrase I cited, that is contextualized in the post as "talking about returning a value. Also, this optimization is "mandatory since C++11", but does not come with C++11: The most of the C++03 compilers already have it, when compiling with `/O2` – Emilio Garavaglia Mar 13 '12 at 20:36