This is my code:
#include <iostream>
using namespace std;
class A
{
int i;
public:
A(int v) : i(v) { }
A(const A& r) : i(r.i) {
cout << "Copy constructor" << endl;
}
A operator=(const A& r) {
cout << "Assignment function" << endl;
return r;
}
void show() {
cout << i << endl;
}
};
int main()
{
A a(1);
A b(2);
a = b;
a.show();
return 0;
}
Value of b
is 2
and value of a
is 1
. In 'main', b
is copied into a
and this the output I get:
Assignment function
Copy constructor
This is understandable, but the output for a.show()
comes out be 1
. I can't understand this. How? Because b
is copied into a using the copy constructor so shouldn't a.i
have the value of b.i
?