class x
{
int a;
public:
x()
{
cout<<"\n\ndefault constructor";
}
x(x& obj)
{
cout<<"\n\ncopy constructor";
}
x fun()
{
x ob;
return ob;
}
};
int main()
{
x ob1;
x ob2=ob1.fun();
return 0;
}
最初,此代碼給了一個錯誤「不匹配函數調用‘X :: X(X)’」, 當我改變了複製構造到常量在C複製構造++
x(const x& obj)
{
cout<<"\n\ncopy constructor";
}
輸出變爲
默認構造函數
默認構造函數
仍然拷貝構造函數不執行....爲什麼?
http://stackoverflow.com/questions/1932700/copy-constructor-not-called-but-compiler-complains-that-theres-no – Mat