Why do templates let you get around incomplete types? I was following an example in a textbook on Nodes, Linked lists and Iterators. I noticed he used a pointer to instances of Lists and Nodes in the Iterator class, but then he also created a method in the List class to return an Iterator for that class. When I tried to implement it myself to save time I didn't template it and chose int data instead, however I would get an error of incomplete type for my Iterator-typed method. When I followed the template notation it worked fine.
template<class Datatype> class Object;
template<class Datatype> class List;
template<class Datatype> class Helper;
template <class Datatype>
class Object {
public:
Object() : m_data(), next(0) {}
Datatype m_data;
Object<Datatype>* next;
};
template <class Datatype>
class List{
public:
List() : m_head(0), m_tail(0) {}
Object<Datatype>* m_head;
Object<Datatype>* m_tail;
int m_count;
Helper<Datatype> getHelper()
{
return Helper<Datatype>( m_head, this);
}
};
template <class Datatype>
class Helper {
public:
Helper( Object<Datatype>* p_node, List<Datatype>* p_list);
Object<Datatype>* m_node;
List<Datatype>* m_list;
};
edit: There may have been some confusion to what my question was referring to. It was referring to the class-typed method
Helper<Datatype> getHelper()
When hard-typed it results in an error of incomplete type. I was wondering how templates got around this? I have some idea that its because a template allows types of different memory sizes then it doesn't handle that methods memory return size until compile-time. I'm quite curious as to how this works, if anyone has an answer I'd appreciate it.