由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 问一个private destructor的问题
相关主题
[合集] 谁给个stack-based allocation 的C++的例子?how much slower: heap vs stack memory allocation?
array allocation in cstatic variable存在heap还是stack?
new一定要和delete配对吗?请教函数 INIT 怎么能free memory
C语言的变量都一定要放在stack上吗?stack/heap corruption
heap 和 stack问题为什么会有recursion stack overflow这个问题?
Windows下多个DLL之间memory allocation问题关于内存泄漏
请问C++ exception后如何清理function stack上的内存资源?菜鸟请教C问题
3 c++ challenge-and-grill questions在子函数内开内存,返回主函数指针然后释放空间是不是很糟糕的(转载)
相关话题的讨论汇总
话题: final2话题: private话题: create话题: class话题: temp
进入Programming版参与讨论
1 (共1页)
s****i
发帖数: 150
1
我想要create instance ONLY on stack,下面的做法是否正确阿?
class temp
{
private:
~temp() {;}
friend class Final;
};
class Final: virtual public temp
{
//Define as usual
}
=============================
我有点搞不清,如果不用friend的话,会怎么样?比如
class final2
{
public:
static final2* Create() { return (new final2()) ; }
private:
~final2(){} ;
} ;
class child : public final2
{
public:
child(){ ;}
} ;
int main()
{
final2 *f ;
f = final2::Create() ;
}
这个时候我的object是在heap上,还是stack上?
P********e
发帖数: 2610
2
你前面temp能是create on heap
private destructor 能保证instance ONLY on HEAP
如果要STACK的话,overload operator new/delete as private, 其实严谨一点
malloc, alloc, realloc都要overload
你的方法是在heap上

【在 s****i 的大作中提到】
: 我想要create instance ONLY on stack,下面的做法是否正确阿?
: class temp
: {
: private:
: ~temp() {;}
: friend class Final;
: };
: class Final: virtual public temp
: {
: //Define as usual

X****r
发帖数: 3557
3
只要operator new就可以了,和malloc之类的无关。

【在 P********e 的大作中提到】
: 你前面temp能是create on heap
: private destructor 能保证instance ONLY on HEAP
: 如果要STACK的话,overload operator new/delete as private, 其实严谨一点
: malloc, alloc, realloc都要overload
: 你的方法是在heap上

1 (共1页)
进入Programming版参与讨论
相关主题
在子函数内开内存,返回主函数指针然后释放空间是不是很糟糕的(转载)heap 和 stack问题
什么是OS Memory management and heap structure?Windows下多个DLL之间memory allocation问题
"brk()" 和 mmap() 有什么区别? (转载)请问C++ exception后如何清理function stack上的内存资源?
one question about overloading operator delete3 c++ challenge-and-grill questions
[合集] 谁给个stack-based allocation 的C++的例子?how much slower: heap vs stack memory allocation?
array allocation in cstatic variable存在heap还是stack?
new一定要和delete配对吗?请教函数 INIT 怎么能free memory
C语言的变量都一定要放在stack上吗?stack/heap corruption
相关话题的讨论汇总
话题: final2话题: private话题: create话题: class话题: temp