I hava a little problem while coding a template class win C++.
The problem is fairly simple: I dn't know whether to do a delete
on the paremetrized type or not, since it could, or it could not, be a pointer.
I have seen this: Destructor in template class c : How to delete field which may be pointer or not pointer?
and I have implemented the first solution, but this requires me to specialize the whole class, so it means I must have 2 classes:
template<class T>
class Node {
private:
T _content;
public:
Node(const T c);
~Node();
};
and
template<class T>
class Node<T*> {
private:
T _content;
public:
Node(const T c);
~Node();
};
I would like to have only the second version and specialise only the destructor as follows:
template<class T>
class Node<T*> {
private:
T _content;
public:
Node(const T c);
~Node();
};
template <class T>
Node<T>::~Node() {
while(!_adjacent.isEmpty()) {
disconnectFrom(_adjacent.first());
}
}
template <class T>
Node<T*>::~Node() {
while(!_adjacent.isEmpty()) {
disconnectFrom(_adjacent.first());
}
delete _content;
}
but then I get the following error:
Node.hpp:43:17: error: invalid use of incomplete type ‘class Node<T*>’
Node.hpp:8:7: error: declaration of ‘class Node<T*>’
Is there any way to specialise only the constructor to avoid having 2 classes (my Node class is much bigger than what I show here)?
Thanks!