Sign in to follow this  

Can operator delete be private?

This topic is 4866 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Can I overload the delete operator as private for a class? I want to prevent it being used outside of a special Cleanup function. I tried overloading the delete operator as private, but then I got this error, both when trying to call new. error C2248: 'Graphics::Image::operator delete' : cannot access private member declared in class 'Graphics::Image' I supposed this was because the new operator calls the delete operator somehow, so I overloaded the new operator for the class, but it still didn't work! What to do? Also, what's the minimum required code in the new/delete operators?

Share this post


Link to post
Share on other sites
I dont have the reference handy, but off the top of my head it doesnt seem right for delete to be private. You would sorta be breaking the language. Instead, just have destructor call your cleanup function. That would be the "proper" way IMO.

Share this post


Link to post
Share on other sites
Yes, you can overloard the delete operator for a class and make it private. The kicker is if you do that, then you effectively make the corresponding new operator for the class private as well. The reason for this is that if the constructor for the class throws after the memory is allocated, then operator delete must be called before the exception propogates up the stack, so any code that calls new must have access to delete.

It's possible to break this dependency by introducing a deliberate asymmetry between the signatures of operator new and operator delete, but this is a bad idea since it can cause your program to leak memory if your constructor throws an exception, and can cause some weird syntatic convoltions during standard use of your class.

A better solution may be to make your destructor private. This prevents delete from being called on pointers to the class, but doesn't hinder operator new in anyway. The downside is that you won't be able to create instances of the class on the stack, but that may not be an issue for you.

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
Quote:
Original post by Oxyd
What about overloading it as public and just do nothing inside...

And deliberately leaking memory is a good idea in how many situations?


What about to write some function that would free the memory up? Or just use free for it? [smile]. Certainly there are lots of ways to free the memory...

And just make the constructor throw no exceptions (SomeClass () throw () {...}).

But, basically, I agree it's stupid idea and the private destructor should be used [smile]

Oxyd

Share this post


Link to post
Share on other sites
Quote:
Original post by Oxyd
Quote:
Original post by SiCrane
Quote:
Original post by Oxyd
What about overloading it as public and just do nothing inside...

And deliberately leaking memory is a good idea in how many situations?


What about to write some function that would free the memory up? Or just use free for it? [smile]. Certainly there are lots of ways to free the memory...

And just make the constructor throw no exceptions (SomeClass () throw () {...}).

But, basically, I agree it's stupid idea and the private destructor should be used [smile]

Oxyd


Maybe he is writing an own memory manager with automatic garbage collection. The application must not delete objects, the garbage collector will be responsible. That's why operator delete should be private.

Share this post


Link to post
Share on other sites
Exactly what nmi said! Anyway, I got around this by using a private destructor.
But I'd still like to know what the minimum required code in the new/delete operators are, should you want to overload them. And what the default ones does.

Share this post


Link to post
Share on other sites
The default (and probably the minimum) funcionality is like as follows:


void *operator new (size_t size)
{
void *mem = malloc (size);
if (mem == 0)
throw std::bad_alloc ();
return mem;
}

void *operator new [] (size_t size)
{
//the same as for new
}

void operator delete (void *mem)
{
if (mem != 0)
free (mem);
}

void operator delete [] (void *mem)
{
//the same as for delete
}



It's generally stupid idea to overload global new and delete (especially, if the funcionality changes dramatically).

Often only class-overloaded new and delete is used. And it's used quite rarely. Probably only in memory managers [smile]

Oxyd

Share this post


Link to post
Share on other sites

This topic is 4866 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this