3 c++ copy-constructor move-semantics copy-elision nrvo
为什么g_Fun()执行return temp它会调用复制构造函数?
class CExample
{
private:
int a;
public:
CExample(int b)
{
a = b;
}
CExample(const CExample& C)
{
a = C.a;
cout<<"copy"<<endl;
}
void Show ()
{
cout<<a<<endl;
}
};
CExample g_Fun()
{
CExample temp(0);
return temp;
}
int main()
{
g_Fun();
return 0;
}
Run Code Online (Sandbox Code Playgroud)