ベクトルを使用して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 . . .
*/
上記のコードに関する私の質問は、追加のデストラクタが呼び出される理由がわからないことです。コンストラクタとデストラクタがいつ呼び出されるかをチェックして、パフォーマンスを理解しようとしています。WindowsXPでVS2008を使用しています。
お時間を割いていただきありがとうございます