CompuServe Messages

C++ question

    20-Oct-92 10:55:59
Sb: C++ question
Fm: Thomas A. Elam 72607,654
To: Greg Comeau@Comeau Cmptg 72331,3421
[Continued] >>I>what does Mr. Stroustrup mean when he talks about turning "unsafe, but generic >>I>list of void* pointers into a … family of type-safe list classes"? (p. 457, >>I>C++ Prog Lang, 2nd ed, Apr 92 corrected printing). >> >>U>He is saying is that you loose typing when you start using a >>U>void * (the void * only possesses the address and the type info is lost >>U>forever as far as the void * itself is concerned). He is also saying that >>U>void * is useful for generic container classes….. >> >>Is this all just to save the extra asterisk (*) that would be necessary in >>a class declaration via the non-intrusive list on pp. 262-267? > >No, has nothing to do what an extra asterisk. void * is just not safe >typing. Consider: > > int *ip; > float *fp; > void *vp; > > ip = fp; // A: what can we say about this? > fp = ip; // B: or this? > > [etc.] I didn't ask my question clearly enough. Please let me try again. I will also try to get Bjarne Stroustrup's BIX address and ask him. There are at least two ways to make a type-safe list class from a list-class template. One is to instantiate a general-purpose template class such as Slist as a list of type-safe pointers. The following is a compilable example: ————————— cut here —————————- struct slink { slink* next; slink() { next=0; } slink(slink* p) { next = p; } }; class slist_base { slink* last; public: void insert(slink*); slist_base() { last = 0; } slist_base(slink* a) { last = a->next = a; } }; void slist_base::insert(slink* a) { if (last) a->next = last->next; else last = a; last->next = a; } [More]