Good morning,
I am working on a C++ application which uses polymorphism and I would like some advice in order to avoid memory leaks. See below the code :
class IItem
{
public:
IItem(){}
virtual void Init() = 0;
virtual ~IItem(){}
};
class ItemA : public IItem
{
private:
int m_a;
public:
ItemA(){ m_a = 0; }
void Init(){m_a = 10;}
virtual ~ItemA(){}
};
class Element
{
public:
int m_a;
int m_b;
IItem* m_pItem;
Element(){}
void Init(IItem* item)
{
m_pItem = item;
}
virtual ~Element(){}
};
class ItemInfo
{
public:
int m_id;
std::string m_name;
Element m_element;
public:
ItemInfo(){}
virtual ~ItemInfo(){}
};
class Test
{
public:
Test(){}
virtual ~Test(){}
void Initialize(std::vector<ItemInfo>* arrayItem)
{
for(int i=0;i<5;i++)
{
arrayItem->push_back(ItemInfo());
arrayItem->back().m_element.Init(new ItemA());
}
}
};
And I am calling in a main program with this line :
Test test;
std::vector<ItemInfo> arrayItemInfo;
test.Initialize(&arrayItemInfo);
In the function Initialize I am using "new ItemA" and my question is how to delete property the memory ?
Thank you in advance for your help.