我有一個程序,它被示出拷貝construtor稱爲額外
#include <cstdlib>
#include <iostream>
using namespace std;
class man{
int i ;
public:
man(){
i=23;
cout << "\n defaul constructir called\t"<< i<<"\n";
}
man (const man & X) {
i = 24;
cout << "\n COPY constructir called\t"<< i<<"\n";
}
man & operator = (man x) {
i = 25;
cout << "\n = operator called\t"<< i<<"\n";
return *this;
}
};
int main(int argc, char *argv[])
{
man x;
cout <<"\n ----------\n";
man y = x;
cout <<"\n ----------\n";
x=y;
return 0;
}
在
defaul constructir called 23
----------
COPY constructir called 24
----------
COPY constructir called 24
= operator called 25
該輸出是奇怪對於x = y的第三呼叫所示的輸出怪異的行爲;
爲什麼當我沒有製作一個新對象但正在使用舊對象時,調用了複製構件的額外打印。
它是介於兩者之間監守臨時對象,如果是我可以在這裏阻止他們....
謝謝@OilCharlesworth。 – MAG