From your posting, its not clear to me what the exact problem is.
If you only want one object to exist and all clients share it, use the
singleton design pattern - see the book.
If the "object is too big to be stack allocated", then design the class so
that you have a wrapper which is ok to be on the stack and the guts which
are
dynamically allocated. In this way, you'll get the benefits of clean up.
If the object should always be allocate do the heap, make the constructor
private, but provide a public new() or a static class member to make one for
you. This works because new() can access the private constructor member ok
in
constructing the object, but the client can't.
Overloading the new() operator in the way you says sounds kind of dangerous,
if I do a new() I expect to put in a delete() somewhere, what would the
semantics
be, no-op, or actually destroy the object, this brings us back to the
singleton pattern.
dt.
----- Original Message -----
From: "<- Chameleon ->" <
[email protected]>
Newsgroups: comp.lang.c++
Sent: Wednesday, May 12, 2004 2:14 AM
Subject: avoid stack creation?
I have a class with overloading operator new. (Because, if an identical
object exists, return a pointer to existed object instead of
a new pointer)
It has no sense (it is dangerous) to allocate an object of that class in stack.
So, is there a trick to avoid stack creation of an object of this class?
I mean, to take compile-time error instead of runtime.
I want to create this class object only in heap.
Thanks.
I have a class with overloading operator new. (Because, if an identical
object exists, return a pointer to existed object instead of