我有这个代码:
class Class {
public:
virtual void first() {};
virtual void second() {};
};
Class* object = new Class();
object->first();
object->second();
delete object;
Run Code Online (Sandbox Code Playgroud)
用Visual C++ 10和/ O2编译并进行反汇编:
282: Class* object = new Class();
00403953 push 4
00403955 call dword ptr [__imp_operator new (4050BCh)]
0040395B add esp,4
0040395E test eax,eax
00403960 je wmain+1Ch (40396Ch)
00403962 mov dword ptr [eax],offset Class::`vftable' (4056A4h)
00403968 mov esi,eax
0040396A jmp wmain+1Eh (40396Eh)
0040396C xor esi,esi
283: object->first();
0040396E mov eax,dword ptr [esi]
00403970 mov edx,dword …Run Code Online (Sandbox Code Playgroud) 假设我有一个这样的类:
#include <new>
struct thing {
thing() = default;
void foo()
{
this->~thing();
new (this) thing();
}
};
Run Code Online (Sandbox Code Playgroud)
是这样调用析构函数然后使用放置新定义的行为重建对象?