以下代码仅打印A::A()
,但不打印A::A(const A&)
or operator=
。为什么?
struct A
{
A() { cout << "A::A()" << endl; }
A(const A& value) { cout << "A::A(const A&)" << endl; }
A& operator=(const A& newValut)
{
cout << "A::operator=" << endl;
return *this;
}
};
A foo()
{
A a; //Ok, there we have to create local object by calling A::A().
return a; //And there we need to copy it, otherwise it will be destroyed
//because it's local object. But we don't.
}
int main()
{
A aa = foo(); //Also there we need to put result to the aa
//by calling A::A(const A&), but we don't.
}
所以这段代码必须打印
A::A()
A::A(const A&)
A::A(const A&)
但事实并非如此。为什么?
我建议没有优化就没有foo()
under的内联。g++