Sign in to follow this  

Preventing a pointer from being deleted

This topic is 4860 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

Is there a way to make the pointer returned by a funtion un-deleteable? example given something like:
FooObject* SomeClass::Func()
{
    return pFoo; // pFoo is a member of SomeClass and holds the address of a memory allocation
}


how do i make this delete statement illegal (not compile)

SomeClass someClass;

delete someClass.Func();


Thanks

Share this post


Link to post
Share on other sites
Could you overload the delete operator for that class?

From there on you could error handle with an exception or something or if you want to generate a compiler error, try something similiar to this: http://www.research.att.com/~bs/bs_faq2.html#no-derivation

Share this post


Link to post
Share on other sites
Using references instead of pointers might be a good idea.

Overloading delete should work too, I guess, but I'm not sure what happens if you cast the pointer into a void pointer and delete it :/

Share this post


Link to post
Share on other sites
If you want all pointers to a certain type of object to be un-deleteable, then you can simply put your destructor in the protected/private section of the class. That way, the only things that can delete the object are things that have access to the protected or private data (the class itself, or friends, or children).

Share this post


Link to post
Share on other sites
Quote:
Original post by Agony
If you want all pointers to a certain type of object to be un-deleteable, then you can simply put your destructor in the protected/private section of the class. That way, the only things that can delete the object are things that have access to the protected or private data (the class itself, or friends, or children).


I like that idea, but i get unresolved externals at linking time. Why would that occur?

Share this post


Link to post
Share on other sites
Quote:
Original post by Mulligan
Quote:
Original post by Agony
If you want all pointers to a certain type of object to be un-deleteable, then you can simply put your destructor in the protected/private section of the class. That way, the only things that can delete the object are things that have access to the protected or private data (the class itself, or friends, or children).


I like that idea, but i get unresolved externals at linking time. Why would that occur?


You still have to define it eventually if you declare it e.g.


struct example {
private:
~example() {}
};

Share this post


Link to post
Share on other sites
Quote:
Original post by Mulligan
I already have the implementation, i just moved the declaration to the private section.


which compiler are you using? can you post your class plus imp and other relevant code?.

Share this post


Link to post
Share on other sites
Linker error is odd. [In MSVC6] It should look something like:

*file*.cpp(108) : error C2248: 'Class::~Class' : cannot access protected member declared in class 'Class'
*file*.h(253) : see declaration of 'Class::~Class'

Just a normal ol' compiler error.

Share this post


Link to post
Share on other sites
Read what agony said again:
Quote:

That way, the only things that can delete the object are things that have access to the protected or private data (the class itself, or friends, or children).

in other words, only a friend, or member function can call delete on the pointer if it's private, or only a member, friend or derived member can call delete if it's protected.

Share this post


Link to post
Share on other sites

This topic is 4860 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