given the following snippet
class Box {
public:
int name;
int length;
};
std::ostream &operator<<(std::ostream &os, Box &box) {
os << "Box name: " << box.name << " and length: " << box.length;
return os;
}
int main() {
Box box1;
box1.name = 1;
Box box2;
box2 = box1;
box1.name = 2;
std::cout << box1 << std::endl;
std::cout << &box1 << std::endl;
std::cout << box2 << std::endl;
std::cout << &box2 << std::endl;
return 0;
}
could someone explain me why it prints the following results?
Box name: 2 and length: 4201248
0x61ff08
Box name: 1 and length: 4201248
0x61ff00
I expected to have the name of box2 equals to 2, since box1 and box2 should be pointing to the same object.