views:

881

answers:

5

I know that you can use a dummy "int" parameter on operator++ and operator-- to override the postfix versions of those operators, but I vaguely recall something about a dummy parameter that you could declare on a destructor. Does anyone know anything about that, and if so, what that dummy parameter did?

This was in my old Turbo C++ tutorial books, which I read when I was a teenager (i.e. a long time ago), so I might be completely misremembering it. That was also very early C++, before it was standardized, so it's possible that it was something Turbo C++-specific.

+4  A: 

Either you are misremembering, or you should try to forget it. Destructors don't have parameters, return types and they shouldn't throw exceptions.

David Nehme
I'd love to forget it, but it'll annoy me until I figure out what I think I'm remembering. :-)
Head Geek
+1  A: 

I swear I've heard the same thing, but the C++ FAQ seems to say that there is no such form.

hark
Yeah, I checked that too (among other places) before I asked. Thanks for the response though, it confirms that I haven't gone completely off the deep end.
Head Geek
+2  A: 

Perhaps you are thinking of placement new?

class MyClass { /* ... */ };

char * raw_mem = new char [sizeof (MyClass)];
pMyClass = new (raw_mem) MyClass;
// ...
pMyClass-->(~MyClass());
delete[] raw_mem;
Thomas L Holaday
I don't think so, but since you mention that, it might have been something related to a specific form of `delete` instead of in the destructor itself.
Head Geek
+4  A: 

You're possibly thinking of the placement and nothrow forms of operator delete, which have the signatures:

void operator delete(void *, void *) throw();
void operator delete(void *, const std::nothrow_t&) throw();
void operator delete[](void *, void *) throw();
void operator delete[](void *, const std::nothrow_t&) throw();

These are never called during normal operation, but would be used in the case where the constructor for an object being constructed with placement new throws an exception. Generally you don't have to define them, since the compiler already called the destructor(s) on the dead object's bases and members, and for placement new there's no memory to be freed. But can exist if you are overloading placement new and need a corresponding operator.

The second argument is not really used, and just distinguishes the signature for the ordinary:

void operator delete(void *)

These aren't special dummy arguments the way the operator++ ones are, though. They're just an instance of the general rule that call to new with extra arguments, such as:

obj = new(x,y,z) Object(a,b,c)

will generate implicit code to clean up from constructor errors that passes those same additional arguments to the operator delete, which will function (approximately) like:

void *raw = operator new(sizeof(Object), x,y,z)
try {
    obj = new(raw) Object(a,b,c);
} catch(...) {
   operator delete(raw,x,y,z);
   throw;
}
puetzk
Ah... yes, I think that's it. I'll have to study that answer further, it's something that I wasn't aware of before. Thanks!
Head Geek
+1  A: 

You're not crazy. I have definitely seen an int parameter in a destructor before. Using HP's compiler on OpenVMS, I compiled a sample program show below. The list of symbols does include an destructor with an int parameter. I can only guess this is compiler specific.

$ create foo.cxx
class foo
{
 ~foo() {}
};

$ cxx foo.cxx

$ type [.CXX_REPOSITORY]cxx$demangler_db.
CX3$_ZN3FOOD1EV31GNTHJ         foo::$complete$~foo()
CX3$_ZN3FOOD2EV30KQI3A         foo::$subobject$~foo()
CX3$_ZN3FOOD9EV36HH9SB         foo::~foo(int)
CXXL$_ZDLPV                    void operator delete(void *)
Hm... thanks, I'll see if I can dig something up about it that way.
Head Geek