Question

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.

Was it helpful?

Solution

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;
}

OTHER TIPS

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

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

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;

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 *)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top