c***y 发帖数: 560 | 1 how to enforce an object instance only be allocated on the stack rather than
the heap at compile time?
thanks a lot. |
l******c 发帖数: 2555 | 2 override operator new?????
than
【在 c***y 的大作中提到】 : how to enforce an object instance only be allocated on the stack rather than : the heap at compile time? : thanks a lot.
|
l******c 发帖数: 2555 | 3 private new operator
than
【在 c***y 的大作中提到】 : how to enforce an object instance only be allocated on the stack rather than : the heap at compile time? : thanks a lot.
|
c***y 发帖数: 560 | 4 what does this mean? this leads to failure of calling new operator?
【在 l******c 的大作中提到】 : private new operator : : than
|
g*******y 发帖数: 1930 | 5 i guess you can pre-allocate some memory in compile time, and then manage
that memory yourself (memory pool alloc/free), and then redefine the new
operator (class specific operator new)
【在 c***y 的大作中提到】 : what does this mean? this leads to failure of calling new operator?
|
B*****t 发帖数: 335 | 6 There are 3 memory storage types for c++ to allocate memory for an object.
1. stack memory for object inside a function.
2. static storage for namespace-scope objects and local static objects
3. heap storage for dynamically-allocated objects.
If the stack in you problem refers to the first type of memory storage,
there is only one method that you could do this, which is defining a
function, allocating memory for an object inside a function, and using it.
You cannot use it outside of a function.
【在 c***y 的大作中提到】 : how to enforce an object instance only be allocated on the stack rather than : the heap at compile time? : thanks a lot.
|
f****4 发帖数: 1359 | 7 effective c++ & more effective c++上有讨论
如果禁止一个对象在stack上生成;把析构函数声明成私有函数
如果只想在heap上生成;构造函数声明成私有;同时提供虚拟构造函数(这里不是指
virtual constructor,而是指用factory patterns生成对象) |
c***y 发帖数: 560 | 8 what's your point?
we want to get an object instance on Stack, your methods are reverse?
【在 f****4 的大作中提到】 : effective c++ & more effective c++上有讨论 : 如果禁止一个对象在stack上生成;把析构函数声明成私有函数 : 如果只想在heap上生成;构造函数声明成私有;同时提供虚拟构造函数(这里不是指 : virtual constructor,而是指用factory patterns生成对象)
|
a****l 发帖数: 245 | 9 To prohibit heap-based objects, we can declare operator new (and operator
delete) as private since it is called by the new operator. |