I am practicing some inheritance and virtual destructors and i keep getting problem with exception being thrown after running my code, where child class destructor works properly but superclass destructor just wont work properly.
I suppose that i have a few misconceptions about destructors since every time i saw virtual destructors, command 'delete' was used outside of destructor actually, but, if i do that, then, what is the point of creating destructor?
#include <iostream>
template <class T>
class List
{
protected:
T *data;
public:
List()
{
data = new T[5];
std::cout << "List constructor!";
}
virtual void putIn()
{
for (int i = 0; i < 4; i++)
std::cin >> data[i];
}
virtual void printOut()
{
for (int i = 0; i < 5; i++)
std::cout << data[i] << std::endl;
}
virtual ~List()
{
delete[]data;
std::cout << "List destructor!";
}
};
template <class T>
class League : public List<T>
{
public:
League()
{
data = new T[5];
std::cout << "League constructor!";
}
virtual void putIn()
{
std::cout << "Enter your values:\n";
for (int i = 0; i < 5; i++)
std::cin >> data[i];
}
virtual void printOut()
{
std::cout << "Your values are:\n";
for (int i = 0; i < 5; i++)
std::cout << data[i] << std::endl;
}
~League()
{
delete[]data;
std::cout << "League destructor!";
}
};
int main()
{
List<char> *p;
League<char> leag;
p = &leag;
p ->putIn();
getchar();
getchar();
}
Everything works just fine, but when program finishes, it says that exception is thrown it points to the base class destructor. Any help appreciated!