C++ question, continued
20-Oct-92 10:57:26
Sb: C++ question, continued
Fm: Thomas A. Elam 72607,654
To: Greg Comeau@Comeau Cmptg 72331,3421
[Continued]
template<class T>
class Tlink : public slink
{
public:
T info;
Tlink(const T& a) : info(a) {}
};
template<class T> class Slist : private slist_base {
public:
void insert(const T& a) { slist_base::insert(new Tlink<T>(a)); }
};
main()
{
Slist<int*> iplist;
}
————————— cut here —————————-
A second way to make a type-safe list class from a list-class
template is to build a list-of-void* class template from the
above Slist, then instantiate the list-of-void* template
class as a list of type-safe pointers. The following is a
compilable example (missing just the class templates from the
first example):
————————— cut here —————————-
template<class T>
class Splist : private Slist<void*> {
public:
void insert(T* p) { Slist<void*>::insert(p); }
};
main()
{
Splist<int> iplist;
int i1 = 1, i2 = 2;
iplist.insert(&i1);
iplist.insert(&i2);
}
————————— cut here —————————-
It looks to me like Stroustrup is saying
Splist<int> iplist;
is better than
Slist<int*> iplist;
Is it better, and, if so, why?
[More]