From: dan on
Hi All,
I've a question regarding function pointer and destructor.
Is it possible to create a typedef param (*myDestructorPointer)(), so
that I can assign
object->~myClassType() to my function pointer variable?

Definitely I have a proxy class, which can wrap several data types
from other libraries, but this data type can have destructor as
protected so I can't
compile if I use delete keyword to destroy wrapped object. I don't
know if other strategies exist

thank you, cheers,
w

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Daniel Krügler on
On 12 Mrz., 21:01, dan <dan...(a)gmail.com> wrote:
> Hi All,
> I've a question regarding function pointer and destructor.
> Is it possible to create a typedef param (*myDestructorPointer)(), so
> that I can assign
> object->~myClassType() to my function pointer variable?

There are no language means to get the address of
a destructor of a class, see [class.dtor]/2:

"A destructor is used to destroy objects of its class type.
A destructor takes no parameters, and no return type can be
specified for it (not even void). The address of a destructor
shall not be taken. [..]"

> Definitely I have a proxy class, which can wrap several data types
> from other libraries, but this data type can have destructor as
> protected so I can't
> compile if I use delete keyword to destroy wrapped object. I don't
> know if other strategies exist

Why don't you declare a special function as
a friend (or make it a static member of your
class), that invokes the destructor on a
given pointer of your class? E.g.

class YourClassType {
protected:
~YourClassType();
public:
static dispose(YourClassType* p) {
if (p) p->~();
}
};

If you need a function object that can
be invoked without arguments, you may
consider

struct Disposer {
private:
YourClassType* p;
public:
Disposer(YourClassType* p) : p(p) {}

void operator()() const {
YourClassType::dispose(p);
}
};

HTH & Greetings from Bremen,

Daniel Kr�gler


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: itaj sherman on
On Mar 12, 10:01 pm, dan <dan...(a)gmail.com> wrote:
> Hi All,
> I've a question regarding function pointer and destructor.
> Is it possible to create a typedef param (*myDestructorPointer)(), so
> that I can assign
> object->~myClassType() to my function pointer variable?
>
> Definitely I have a proxy class, which can wrap several data types
> from other libraries, but this data type can have destructor as
> protected so I can't
> compile if I use delete keyword to destroy wrapped object. I don't
> know if other strategies exist

{ edits: quoted signature & banner removed. please keep readers in mind when
quoting. -mod }

I see 2 problems:

1)
You said you wanted to delete the object. Even if you would call the
destructor in some way, it is not the same as operator delete. What
delete does, is calling the destructor and then disposing the memory
allocated by operator new that was called to create this object.

2)
You never mentioned how the objects were created. Usually, the when
and how to allocate+create and destruct+deallocate of certain objects
in a module, are designed together and should be clearly defined in
the module's interface, and documentation (if you got good
documentation).
The designer of the library should have decided on how objects are to
be created and destroyed, and probabely added some other functions to
the library, that will do so, and the documentation on it. Check the
documentation.
If you are the designer of the library, or someone that works with
you, get him to do the above. Then, in order to destroy objects, use
the interface that the library exposes.

If a designer of a class in a library declared its destructor
protected, then it means that users of this library cannot destruct
instances of this class (or of more derived classes) through a pointer
to this class directly. He probabely have had a good reason to do it.
In this case, if you somehow trick around this and destroy the object,
it will have undefined results.
One reason I can think of, is that this class is a base class of a few
other classes, and because this base's destructor could not be virtual
(for whatever reason), it is not allowed (by c++ standard) to directly
invoke this destructor on an instance of a more derived class. There
are many other reasons, some are actually strictly for interface
usability.

If for example (but not necessariliy a good one) the library expects
you to derive this base class, and create instances of your derived
classes, then you can use operator new and operator delete. But you
will have to use operator delete on a pointer to your derived class
(subobject of the same object of maybe a more derived class), and not
the library's base. Make sure that the level on which you delete, is
either the most derived class of the instance, or has a virtual
destructor.

itaj


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]