在c++中调用附加的析构函数
Additional destructors are called in C++
我在c++中使用vector
编写了以下代码#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
class DataValue {
public:
DataValue() { std::cout << "DataValue constructor called" << std::endl; }
DataValue(DataValue const& other) { cout << "DataValue copy constructor called" << std::endl; }
~DataValue() { std::cout << "DataValue destructor is called" << std::endl; }
private:
};
class ItemDataHistory {
public:
ItemDataHistory() { std::cout << "ItemDataHistory constructor called" << std::endl; }
ItemDataHistory(ItemDataHistory & other) { std::cout << "ItemDataHistory copy constructor called" << std::endl; }
~ItemDataHistory() { std::cout << "ItemDataHistory destructor called" << std::endl; }
std::vector<DataValue>& GetVecDataValues() { return m_vecDataValues; }
private:
std::vector<DataValue> m_vecDataValues;
};
class DataReply {
public:
std::vector<ItemDataHistory>& GetItemDataHistories() { return m_vecItemData; }
private:
// The list of DataValue
std::vector<ItemDataHistory> m_vecItemData;
};
void main()
{
DataValue dv1, dv2, dv3;
ItemDataHistory itmDH;
itmDH.GetVecDataValues().push_back(dv1);
itmDH.GetVecDataValues().push_back(dv2);
itmDH.GetVecDataValues().push_back(dv3);
return;
}
/*
DataValue constructor called
DataValue constructor called
DataValue constructor called
ItemDataHistory constructor called
DataValue copy constructor called
DataValue copy constructor called
DataValue copy constructor called
**DataValue destructor is called** // why this destructor is called? where constructor for this.
DataValue copy constructor called
DataValue copy constructor called
DataValue copy constructor called
**DataValue destructor is called // why this destructor is called? Where is constructor for this.
DataValue destructor is called** // why this destructor is called? Where is contructor for this.
ItemDataHistory destructor called
DataValue destructor is called
DataValue destructor is called
DataValue destructor is called
DataValue destructor is called
DataValue destructor is called
DataValue destructor is called
Press any key to continue . . .
*/
我对上面代码的问题是,我不明白为什么要调用额外的析构函数。我试图通过检查何时调用构造函数和析构函数来了解性能。我在windows XP中使用VS 2008。
感谢您的时间和帮助
push_back
可能想要增加vector的大小,这意味着旧的元素被复制到一个新的更大的位置,然后销毁。
作为对其他答案的补充:为了获得良好的可视化效果,只需添加
itmDH.GetVecDataValues().reserve(3);
在您使用push_back
之前,它将导致您首先期望看到的输出:
在不使用reserve
的情况下使用代码并检查每个push_back
之前和之后的容量(liveworkspace demo #2)也可能很有指导意义:
DataValue constructor called
DataValue constructor called
DataValue constructor called
ItemDataHistory constructor called
0
DataValue copy constructor called
1 //reallocation of vector happens
DataValue copy constructor called //copy first element from old memory location
DataValue copy constructor called //copy the element to be inserted
DataValue destructor is called //destroy element at old memory location
2 //reallocation of vector happens
DataValue copy constructor called //copy first element from old memory location
DataValue copy constructor called //copy second element from old memory location
DataValue copy constructor called //copy the element to be inserted
DataValue destructor is called //destroy first element at old memory location
DataValue destructor is called //destroy second element at old memory location
4
...
相关文章:
- 析构函数调用
- 在具有向量的类构造函数中进行析构函数调用
- 从 c++ 中派生类的析构函数调用虚函数
- C++析构函数调用两次,堆栈分配的复合对象
- C++ 在析构函数调用之前删除的动态成员数组
- 析构函数调用c++中的一个向量
- Singleton模式中的手动析构函数调用:调用多次
- 从内部类的析构函数调用虚拟函数
- 与 boost odeint 集成期间的析构函数调用
- 堆栈展开如何与析构函数调用有关?
- C++:优化析构函数调用
- 以逗号分隔的表达式中的析构函数调用
- GCC 9.1 返回 void& 作为显式析构函数调用的结果类型。这是一个错误吗?
- 从C++中的虚拟析构函数调用虚拟方法
- 从指针返回对象时出现意外的析构函数调用
- 使用 decltype 显式析构函数调用
- C++析构函数调用了错误的对象
- 了解虚拟函数和析构函数调用
- 多重继承析构函数调用他自己和父析构函数?c++
- 析构函数调用表单不适当的库