J
janzon
Consider the code below. The output is the following two lines:
0xbfc78090
0xbfc780a0
This proves that the variable m in main() is not the very same instance
of MyClass as temp_m in hello(). Hence (?) m is created as copy of
temp_m. But the copy constructor is not called. Contradiction. Where am
I thinking incorrectly?
#include <iostream>
using namespace std;
class MyClass
{
public:
MyClass () {}
MyClass (const MyClass& m)
{
cout << "Copy constructor called!" << endl;
}
};
MyClass hello()
{
MyClass temp_m;
cout << &temp_m << endl;
return temp_m;
}
int main()
{
MyClass m;
m=hello();
cout << &m <<endl;
}
0xbfc78090
0xbfc780a0
This proves that the variable m in main() is not the very same instance
of MyClass as temp_m in hello(). Hence (?) m is created as copy of
temp_m. But the copy constructor is not called. Contradiction. Where am
I thinking incorrectly?
#include <iostream>
using namespace std;
class MyClass
{
public:
MyClass () {}
MyClass (const MyClass& m)
{
cout << "Copy constructor called!" << endl;
}
};
MyClass hello()
{
MyClass temp_m;
cout << &temp_m << endl;
return temp_m;
}
int main()
{
MyClass m;
m=hello();
cout << &m <<endl;
}