雖然析構函數不是類的函數,但是我們還是有辦法獲得其地址的。
#include <iostream>
using namespace std;
template <typename T>
static void* Destruct()//得到T析構函數的地址并返回
{
T *p;
goto getDesAddr;
desAddr:
p->~T();
#ifdef _WIN32 //_MSC_VER //intel格式匯編,windows 平臺
#ifdef _MSC_VER
__asm{
ret
getDesAddr:
push eax
mov eax,desAddr //save the address of T::~T()
mov p,eax
pop eax
}
#endif
#endif
return (p);
}
typedef void(*Fndes)();
static void executeDestruct(void *addr)//執行addr指向的析構函數
{
Fndes exe=reinterpret_cast<Fndes>(addr);
exe();
}
class a{
public:
~a(){
cout<<"~a"<<endl;
}
};
void main()
{
void*p=Destruct<a>();
executeDestruct(p);
}