Ok, sorry to bother but it seems I don't understand this properly ^^"
I have a simple class, SLListNode, as follows
template <class D>
class SLListNode {
public:
static int Count;
D Data;
SLListNode *Next;
SLListNode(void);
~SLListNode(void);
};
template <class D>
int SLListNode<D>::Count = 0;
template <class D>
SLListNode<D>::SLListNode(void) {
Next = NULL;
Count++;
if(dbg) {
cout << endl << "SLListNode of type '" << typeid(D).name() << "' Constructed. Number of SLListNodes of this type now is " \
<< Count << "." << endl;
}
return;
}
template <class D>
SLListNode<D>::~SLListNode(void) {
Count--;
if(dbg) {
cout << endl << "SLListNode of type '" << typeid(D).name() << "' Destructed. Number of SLListNodes of this type now is " \
<< Count << "." << endl;
}
return;
}
and another class I wanted to inherit from SLListNode:
template <class D>
class DLListNode : public SLListNode<D> {
public:
DLListNode *Prev;
DLListNode(void);
~DLListNode(void);
};
template <class D>
DLListNode<D>::DLListNode(void) {
Prev = NULL;
if(dbg) {
cout << endl << "DLListNode of type '" << typeid(D).name() << "' Constructed. Number of DLListNodes of this type now is " \
<< Count << "." << endl;
}
return;
}
template <class D>
DLListNode<D>::~DLListNode(void) {
if(dbg) {
cout << endl << "DLListNode of type '" << typeid(D).name() << "' Destructed. Number of DLListNodes of this type now is " \
<< Count << "." << endl;
}
return;
}
I created an object with: DLListNode<int> ob1;
and I thought it would inherit from the base class its own Next pointer, its own Data variable, its "own" static variable Count and that the Base class constructor and destructor were to be called before and after respectively its own.
However, it complains about missing Count in DLListNode class... has a static variable of the base class to be redefined in each derived class?
I had to add static int Count;
in the class definition and
template <class D>
int DLListNode<D>::Count = 0;
outside.
And again, the base class constructor (that is called, I see the debug infos printed) does not increment it! Neither does the destructor...
Is the Next pointer initialized at all? I added initialization only to the new Prev pointer - should I reinitialize the first one in the derived class constructor? Evidently I didn't understand how this works.. I will be very grateful if someone does enlighten me a bit :S
Anticipated thanks :)