How to disable regular constructor and only allow new?

P

Peng Yu

Hi,

I want to make sure objects of a class are always created in the heap
rather than the stack. Could anybody let me how to do so?

Regards,
Peng
 
G

Goran

Hi,

I want to make sure objects of a class are always created in the heap
rather than the stack. Could anybody let me how to do so?

Regards,
Peng

class on_heap;
typedef auto_ptr<on_heap> ap_on_heap;
class on_heap
{
private:
on_heap(params) {...}
public:
static ap_on_heap create(params)
{
return ap_on_heap(new on_heap(params));
}
};

If you want to derive from on_heap, make constructors protected.

Goran.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top