Class A
{
public:
NullIt()
{
this = NULL;
}
Foo()
{
NullIt();
}
}
A * a = new A;
a->Foo();
assert(a); //should assert here
Is there a way to achieve this effect, memory leaks aside?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
No. The object knows nothing about the external references to it (in this case, “a”), so it can’t change them.
If you want the caller to forget your object, then you can do this:
When you call Release, you pass in the pointer you hold to the object, and it NULLs it out so that after the call, your pointer is NULL.
(Release() can also “delete this;” so that it destroys itself at the same time)