我们可以通过类指针显式调用析构函数,为什么不能调用构造函数?有任何想法吗?
#include <iostream>
class Con {
public:
Con( int x ) : x( x ) {
}
private:
int x;
};
int main() {
Con* c = new Con( 1 );
//c->Con( 2 ); //illegal
c->~Con(); // ok!
delete c;
}
谢谢。
c->~Con();
可能没问题,但是下一行的delete c;
不行,因为它会尝试再次调用同一个析构函数。 - Cubbic->~Con()
时有些粗心。 - roxrook