#include <iostream>
struct Human {
public:
std::string Name;
int* Age;
Human(std::string Name, int Age): Name{Name}, Age{&Age} {}
void Print() {
std::cout << Name << " " << Age << "\n";
}
};
int main() {
Human a = Human("John", 35);
Human b = Human("Brook", 20);
Human c = Human("Lamy", 90);
Human d = Human("Ed", 5);
a.Print();
b.Print();
c.Print();
d.Print();
}
/* Output
John 0x7bfc80
Brook 0x7bfc80
Lamy 0x7bfc80
Ed 0x7bfc80
*/
How come all the age value addresses are the same in the ouput? How am I able to send a constant value into something that will take its address? (check parameterized constructor) A constant has no well defined position in memory so how is that acceptable? Is the variable int* Age not being duplicated for each object creation?
Thank you!