Wyj*_*jun 2 c++ memory-address
看看这段代码:
class Test
{
//
};
Test TestAddress()
{
Test test;
cout << "Object Address in function: " << (int)&test << endl;
return test;
}
int IntAddress()
{
int test;
cout << "Integer Address in function: " <<(int)&test << endl;
return test;
}
int main() {
int x = IntAddress();
cout << "Integer Address in Main: " <<(int)&x << endl;
Test object = TestAddress();
cout << "Object Address in Main: " <<(int)&object << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出是:
功能中的
整数地址:1076679252 Main中的整数地址:1076679220
函数中的
对象地址:1076679221函数中的对象地址:1076679221
有人可以解释我为什么会这样,当我按值返回一个对象时,我会在函数和main中收到相同的地址.但是当我用Integer做同样的事情时,这个地方是不同的?