Code:
#include <iostream> #include <cstdlib> using namespace std; class myclass { public: int var; myclass(int i) { cout << "Obuchnuy konstructor.\n"; var = i; } myclass(const myclass &obj) { cout << "Konstructor kopii.\n"; cout << "Kk: " << obj.var << endl; } ~myclass() { cout << "Destructor.\n"; } }; myclass f() { myclass ob(5); cout << ob.var << endl; return ob; } int main() { myclass a(10); cout << a.var << endl; a = f(); cout << a.var << endl; system("pause"); return 0; } MVS 2013 Conclusion:
Obuchnuy konstructor. 10 Obuchnuy konstructor. 5 Konstructor kopii. Kk: 5 Destructor. Destructor. -858993460 Those. The copy constructor returned the garbage to main ().
If you compile with mingw and the -fno-elide-constructors key (otherwise the copy constructor is ignored), the output is almost the same, only the garbage is different.
The question is why?