如下因素代码显示不期望的输出:STL Vector是否调用未分配对象的析构函数?
class test
{
public:
test()
{
std::cout << "Created" << (long)this << std::endl;
}
~test()
{
std::cout << "Destroyed" << (long)this << std::endl;
}
};
int main(int argc, char** argv)
{
std::vector<test> v;
test t;
v.push_back(t);
return EXIT_SUCCESS;
}
执行时它显示:
Created-1077942161
Destroyed-1077942161
Destroyed674242816
我认为第二个“破坏”输出不应该存在。当我不使用该向量时,结果是一个Created和一个Destroyed行如预期。这种行为是否正常?
(这与GCC编译FreeBSD系统上)
+1 [SSCCE](http://sscce.org/) – Flexo
要打印指针,最好将其转换为void指针:'std :: cout <<(void *)this << std :: endl ;'。 –
同时返回'EXIT_SUCCESS'是可选的。你可以在'main'中省略返回值,它会返回'0'(这将变成任何意味着你的平台的“正常终止”) –