CompuServe Messages

#C++ question

    22-Oct-92 15:41:32
Sb: #29490-#C++ question
Fm: Greg Comeau@Comeau Cmptg 72331,3421
To: Greg Comeau@Comeau Cmptg 72331,3421
[Continued] So, really then, we don't have two ways to make a class from a template here, but instead a way to have object lists and pointer lists. >It looks to me like Stroustrup is saying > Splist<int> iplist; >is better than > Slist<int*> iplist; >Is it better, and, if so, why? Having just read p255-265 of S2, I don't see him saying either of these two things. He gives the proper explanation and time to each thing. For instance, he spends much time on the non-pointer, and then in the sake of specialization mentions "Since lists of pointers are so useful it is a good idea to name them specifically." This is where you get the Splist from. Re his comment on p457 "Splist turns the unsafe, but generic, list of void * pointers into a much more useful family of type-safe list classes." Notice these "magic" in Splist. void* by itself it not typesafe. That was why I showed that list of different pointer assignment in my last messages. If you pull something out of a void * in C++ you must cast it. +++You are only supposed to pull out of a void * the "same type" that put into it.+++ The language doesn't inforce this though and so it is up to your discpline. Like the code I showed: "void *vp; int *ip; float *fp; vp = ip; fp = (float*)vp;' is most likely an error at runtime even with the cast. The Splist is going to throw typing atop the list because we must feed the type into Splist when we create on of them. At that type should be honor by the member functions. For instance, notice how insert accepts a T and how get returns a T. This allows honoring of +++ and now the compiler will complain if something is not right. Further, since Splist is built upon Slist<void *>, there does not need to be specific Slist <T? *> member function for every type that your program uses. [More]