The writing code is as follows:
#include <iostream>
using namespace std;
class CMyData
{
public:
CMyData(int Param):m_nData(Param)
{
cout << "CMyData(int)" << endl;
}
CMyData(const CMyData &rhs): m_nData(rhs.m_nData)
{
cout << "CMyData( const CMyData & ) " << endl;
}
CMyData(const CMyData &&rhs) : m_nData(rhs.m_nData)
{
cout << "CMyData(const CMyData && ) " << endl;
}
~CMyData()
{
cout << "~CMyData()" << endl;
}
operator int() { return m_nData; }
CMyData operator+(const CMyData & rhs)
{
CMyData result(0);
result.m_nData = this->m_nData + rhs.m_nData;
return result;
}
CMyData& operator=( const CMyData & rhs)
{
m_nData = rhs.m_nData;
return *this;
}
private:
int m_nData = 0;
};
int main(void)
{
cout << "***Begin***" << endl;
CMyData a(0), b(3), c(4);
a = b + c; /// at this point.
cout << a << endl;
cout << "*** end *** " << endl;
return 0;
}
After b+c operation, it seems that a moving generator should be created in the process of substituting it with a, but it cannot be created. Why is that?
c++
When a=b+c
and assign
the result of b+c
is a
, I think the Copy Constructor is the question for to call why not?
Similar questions were then asked in
In summary, the return
value of one function is related to optimization for assign
to another variable.
© 2024 OneMinuteCode. All rights reserved.