I've defined three classes, as they can be seen downwards. B is a derived class from A. C is a register class containing A and B objects in an unknown amount. For this reason, I'm using a pointer array dynamically allocated that will store each dynamically allocated object's reference. For example, I created a two-member collection, and printed out the member variables on the output. Despite deleting them (objects first, then the pointer array) afterwards, memory leak is still detected. Can I get some help, cause I really don't see what I'm doing wrong?
#include <iostream>
#include <string>
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
using namespace std;
class A {
protected:
int a;
public:
A(int pa) { a = pa; }
A(const A& theOther) { this->a = theOther.a; }
virtual void print() const { cout << a << endl; }
};
class B: public A {
protected:
int b;
public:
B(int pa, int pb): A(pa), b(pb) {}
B(const B& theOther): A(theOther.a), b(theOther.b) {}
void print() const {
A::print();
cout << b << endl;
}
};
class C {
public:
unsigned elementNum;
A** pData;
void createCollection() {
cin >> elementNum;
pData = new A * [this->elementNum];
}
~C() {
for (unsigned i = 0; i < this->elementNum; i++) {
delete pData[i];
}
delete[] pData;
}
};
int main(void) {
C REG;
REG.createCollection();
REG.pData[0] = new A(2);
REG.pData[1] = new B(3, 4);
REG.pData[0]->print();
REG.pData[1]->print();
_CrtDumpMemoryLeaks();
return 0;
}