类中的成员变量在哪里分配? [英] Where does the member variable inside a class allocated?
本文介绍了类中的成员变量在哪里分配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个类
class Sample
{
string strName;
};
如果我使用 Sample * obj = new Sample ()
在哪里创建了字符串 strName
? c> Sample obj = new Sample()
/ code>将无法编译。
如果你的意思是 Sample * obj = new Sample()
,那么它就在动态存储/ p>
当调用 delete obj
时,字符串将被自动删除,即使它在堆上。 >
I have a class
class Sample
{
string strName;
};
If i create an object for the Sample using Sample *obj = new Sample()
where does the string strName
created? is it in stack or in heap?
解决方案
Nowhere, because Sample obj = new Sample()
won't compile.
If you mean Sample* obj = new Sample()
, then it's in dynamic storage (heap).
When you call delete obj
, the string will be deleted automatically, even if it's on the heap.
这篇关于类中的成员变量在哪里分配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文