通过值传递函数的返回对象是否合法?我有一个函数,A::getA()
它按值返回一个对象。在同一行中引用此值是否合法,请参见该行
b.processA(a.getA());
请在下面查看我的代码:
class A
{
public:
int a;
std::list<int*> m_list;
A(int a)
{
this->a =a;
}
A(A& _a)
{
this->a =_a.a;
m_list.push_back(&a);
}
A getA()
{
A localA(20);
localA.m_list.push_back(&localA.a);
return localA;
}
};
class B
{
public:
char b;
B(char b)
{
}
void processA(A& a)
{
a.a = 1;
processA2(a);
}
void processA2(A& a)
{
a.a = 2;
}
};
void main()
{
B b('a');
A a(11111);
//************
// IS THE FOLLOWING LINE LEGAL??
// I mean, is it legal to pass the return object of the function by value
//************
b.processA(a.getA());
}