473,408 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

override delete operator to set to NULL

Hello,

I am trying to override the delete operator so that it set 0 to my
object. Is this possible ?

I am used to do:

obj *a = new obj;
....
delete a;
a = 0; //prevent any misuse.

But I am looking for a transparent way to do it.

Thanks,
Mathieu
Jul 22 '05 #1
3 1890
Mathieu Malaterre <mm******@nycap.rr.com> wrote in news:rKK0d.22388
$2*******@twister.nyroc.rr.com:
Hello,

I am trying to override the delete operator so that it set 0 to my
object. Is this possible ?

I am used to do:

obj *a = new obj;
...
delete a;
a = 0; //prevent any misuse.

But I am looking for a transparent way to do it.

Thanks,
Mathieu


This would be of dubious value since you can have multiple variables
pointing to the same object, and you would also have to handle the case
where the pointer is not even stored in a variable at all, e.g.

delete ptr + 1;

If you still wanted to go ahead with the idea, you probably can't do it
by overloading operator delete. You would have to introduce a smart
pointer type that overrides ->.

Gregg
Jul 22 '05 #2
Mathieu Malaterre wrote:
I am trying to override the delete operator so that it set 0 to my
object. Is this possible ?

I am used to do:

obj *a = new obj;
...
delete a;
a = 0; //prevent any misuse.


The need to set a pointer to NULL, after delete, is a C++ "Design Smell".
That means when you notice it, you have an indicator of poor design
elsewhere.

In this case, how can you contrive to make your 'a' _always_ go out of scope
just after deleting it? Going out of scope and disappearing is the best way
a variable has to prevent others from messing with it.

(Hint: Look up "Resource Acquisition Is Initialization".)

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces
Jul 22 '05 #3
Mathieu Malaterre <mm******@nycap.rr.com> wrote:
I am trying to override the delete operator so that it set 0 to my
object. Is this possible ?

I am used to do:

obj *a = new obj;
...
delete a;
a = 0; //prevent any misuse.

But I am looking for a transparent way to do it.


template < typename T >
void DELETE( T*& p ) {
delete p; p = 0;
}

template < typename T >
void DELETE_ARRAY( T*& p ) {
delete [] p; p = 0;
}

However, you would be far better off to use a smart pointer:

auto_ptr<obj> a( new obj );

// now you don't have to delete it and don't have to
// worry about preventing misuse, after it is deleted.
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: foo | last post by:
I'm creating a debug class called debug_mem_allocation for the purpose of finding memory leaks. I used macro's to replace the new and delete operators. My problem is with trying to replace the...
1
by: Nimmi Srivastav | last post by:
There's a rather nondescript book called "Using Borland C++" by Lee and Mark Atkinson (Que Corporation) which presents an excellent discussion of overloaded new and delete operators. In fact there...
2
by: Shark | last post by:
Hi, if we need to change the behavior of operator new, it is called overriding or overloading? My other question is, if we change the behavior of operator new, do we use malloc to do that or we use...
5
by: meng.frank | last post by:
If I want to use std::vector or std::list for my class MyNewClass, which operators should I override? Maybe operator new and operator delete? If I want to use MyNewClass in std::set or std::map,...
52
by: Jim Langston | last post by:
I wanted to do an operator override for but couldnt' figure out the syntax. I tried this (code that doesn't compile commented out with //: class CMyBitmap { public: CMyBitmap( int Rows, int...
10
by: jeffjohnson_alpha | last post by:
We all know that a new-expression, foo* a = new foo() ; allocates memory for a single foo then calls foo::foo(). And we know that void* p = ::operator new(sizeof(foo)) ; allocates a...
15
by: LuB | last post by:
I am constantly creating and destroying a singular object used within a class I wrote. To save a bit of time, I am considering using 'placement new'. I guess we could also debate this decision -...
8
by: Rahul | last post by:
Please read the following code class Test{ public: void * operator new (size_t t) { return malloc(t); } void operator delete (void *p) { free(p); } };
4
by: mail.dsp | last post by:
Suppose in a class we overload four operators: operator new operator delete operator new operator delete class Test{ public: void * operator new (size_t t){ cout<<"\nCalling... new";
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.