Topic: overloading delete
Author: sj@aracnet.com (Scott Johnson)
Date: 1996/08/28 Raw View
[Note to moderator: This post was accidentally misdirected to
comp.lang.c++; I'm reposting it here because it is more on-topic for this
newsgroup. I hope that's OK..... Thanks, Scott]
In a previous post, I mentioned the desire to overload dynamic_cast for
class types (in particular, a smar pointer implementation). I gave a good
approximation (called Dynamic_cast), and laid out my case. The other
thing I'd like to do with my smart pointer is to overloade delete.
Not operator delete, mind you. The keyword delete itself.
For example, I'd like to be able to do
void foo( void )
{
Pointer< Bar > bar = new Bar;
// blah blah
delete bar;
}
One way is to have an implicit cast from Pointer<Bar> to Bar *. But there
are good reasons not to do that.
Since delete is only defined for pointers, why not allow overloading of it
for classes: In other wrods, my smart pointer class can look like:
template <class T>
class Pointer {
private:
T* ptr_;
public:
delete () { delete ptr_; }
};
Notice that no operator keyword appears, just "delete". Unlike operator
delete, which is a static member function, delete is a nonstatic member
function. No return type is specified (or the return type shall be
required to be void.)
We can have a delete[] function as well, and delete should be able to take
parameters to handle the various placement cases, if placement delete does
make its way into the language.
In short, if the delete keyword is followed by a pointer, the thing which
is pointed to has its destructor called, followed by its opearotr delete
(if it exists, otherwise the global operator delete). If the delete
keyword is followed by a class instance, than the overloaded delete (and
not operator detele) is called...which can do whatever it wants.
Scitt "Geez, I'm a pain, ain't I?" Johnson
--
/--------------------------------------------------------------------------\
|Scott Johnson -- Professional (sometimes) SW Engineer and all-purpose Geek|
|I don't speak for nobody but myself, which everyone else is thankful for |
\--------------------------------------------------------------------------/
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]