在C ++中调用其他析构函数

时间:2013-03-06 08:47:49

标签: c++ stl

我使用vector

在C ++中使用以下代码
#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。

感谢您的时间和帮助

2 个答案:

答案 0 :(得分:3)

push_back可能想要增加向量的大小,这意味着将旧元素复制到一个新的更大的位置然后销毁。这可能就是这里发生的事情。

答案 1 :(得分:2)

作为其他答案的补充:要获得良好的可视化效果,只需添加

即可
itmDH.GetVecDataValues().reserve(3);

在您push_back之前,它会产生您希望首先看到的输出:liveworkspace demo

在没有reserve的情况下使用您的代码并检查每个push_backliveworkspace 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
...