[英]how Objects in C++ stored? in heap or stack?
#include <string>
#include <iostream>
class Type {
public:
int x;
Type(int a)
{
x = a;
}
Type(const Type& type1)
{
std::cout << "copy" << std::endl;
}
};
Type Func()
{
Type s(1);
std::cout << &s << std::endl;
return s;
}
int main()
{
Type c = Func();
std::cout << &c << std::endl;
std::cout << c.x << std::endl;
system("pause");
}
C++ 為堆或堆棧中的對象分配內存? 在 Func 中,假設 "s" 是在棧中分配的,那為什么不解構呢? 為什么我仍然可以在 main() 中調用它。 我很困擾。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.