avoid stack creation?

G

Guest

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.
 
J

John Harrison

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.

Simple trick is to make all the constructors private and create with a
friend function

class X
{
friend X* new_x();
private:
X();
public:
...
};

X* new_x() { return new X(); }

john
 
G

Guest

I have a class with overloading operator new. (Because, if an identical
object exists, return a pointer to existed object instead of

Simple trick is to make all the constructors private and create with a
friend function

class X
{
friend X* new_x();
private:
X();
public:
...
};

X* new_x() { return new X(); }


this method has a problem:
destructor never run!
 
J

John Harrison

this method has a problem:
destructor never run!

That will be because you never called delete.

X * xp = new_x();
....
delete xp; // destructor runs here

john
 
D

Dave Townsend

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
 
M

Michiel Salters

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.

This is a very very bad idea. If I call new twice, I *will* call
delete twice. This will break your operator new, because it didn't
allocate memory twice.

The correct implementation is not to use pointers at all, here.
Instead, create a X_handle class. This class can be freely copied,
created, etcetera. Internally, they hold a pointer to the real
X object. Each X object holds an X_handle_count member. This count
is controlled by the X_handle ctors and dtors. When the count
reaches 0, the last X_handle dtor deletes the X.

Because the X_handle has sizeof(X*), it can be copied around,
stack allocated, etcetera. The real X is always on the free store.
X doesn't have public ctors or dtors, but X_handle is a friend.

Regards,
Michiel Salters
 
G

Guest

I have a class with overloading operator new. (Because, if an
This is a very very bad idea. If I call new twice, I *will* call
delete twice. This will break your operator new, because it didn't
allocate memory twice.

The correct implementation is not to use pointers at all, here.
Instead, create a X_handle class. This class can be freely copied,
created, etcetera. Internally, they hold a pointer to the real
X object. Each X object holds an X_handle_count member. This count
is controlled by the X_handle ctors and dtors. When the count
reaches 0, the last X_handle dtor deletes the X.

Because the X_handle has sizeof(X*), it can be copied around,
stack allocated, etcetera. The real X is always on the free store.
X doesn't have public ctors or dtors, but X_handle is a friend.


I use this method earlier with a template class:
class SharedObject<C>
which contains a class (any class).


Now I use a class with private constructor (so, I cannot run "new" or stack allocation)
and it have a public method
Texture2D *Texture2D::LoadTexture(..............);
which maybe allocate a new object or maybe return a pointer to a previous allocated object (increase counter).

And delete:

void Texture2D::eek:perator delete(void *obj)
{
Texture2D *tex = (Texture2D*) obj;
if (--tex->count == 0) free(obj);
}
 

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
474,169
Messages
2,570,919
Members
47,459
Latest member
Vida00R129

Latest Threads

Top