我遇到了一个例子:
#include <iostream>
#include <stdexcept>
using namespace std;
class A{
public:
A():m_n(m_object_id++){}
~A(){cout << m_n;}
private:
const int m_n;
static int m_object_id;
};
int A::m_object_id=0;
int main()
{
A * const p = new A[3];
A * const q = reinterpret_cast<A * const> (new char[3*sizeof(A)]);
new (q) A;
new (q+1)A;
q->~A();
q[1].~A();
delete [] reinterpret_cast<char *>(q); // -> here
delete[] p;
cout << endl;
return 0;
}
输出:
34210
有人可以解释一下delete [] reinterpret_cast<char *>(q); // -> here
,它在做什么并产生任何输出吗?
编辑
我的问题delete [] reinterpret_cast<char *>(q);
不是打电话~A(){cout << m_n;}
,delete[] p;
为什么会这样?