#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
*/
Run Code Online (Sandbox Code Playgroud)
为什么输出中所有年龄值地址都相同?我如何才能将常量值发送到将获取其地址的对象中?(检查参数化构造函数)常量在内存中没有明确定义的位置,那么这如何可以接受?每次创建对象时变量 int* Age 是否都不会重复?
谢谢你!