Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!think.com!barmar From: barmar@think.com (Barry Margolin) Newsgroups: comp.lang.c++ Subject: Re: Comparison functions for qsort() and bsearch() Keywords: Sun, C++ Message-ID: <1991Jan2.191630.18581@Think.COM> Date: 2 Jan 91 19:16:30 GMT References: <3069@lupine.NCD.COM> <277640DD.4A70@tct.uucp> <1990Dec26.160636.15566@clear.com> Sender: news@Think.COM Organization: Thinking Machines Corporation, Cambridge MA, USA Lines: 40 In article <1990Dec26.160636.15566@clear.com> rmartin@clear.com (Bob Martin) writes: > I dissagree. As Ron said in his article. It is always possible > to cast a pointer to anything to a pointer to void. But it is > not guaranteed that you can cast a pointer to void back to > its original. This was a point I made in my article a week or two ago, and I was corrected. You can always cast a pointer to void back to its original type. However, if you cast T* to void*, you can't always cast the void* to *. > Case in point is a class derived from multiple bases: > > class AB : public A, public B; > > Now take the following variable: > AB *ab; > > The following two casts work just fine: > (A *)ab; (B *)ab; > > But the following, though it may compile, is likely not to work properly > because it miscalculates the address of at least one of the base classes. > (A *)(void *)ab; or (B *)(void *)ab; But the following do work: (A *) (AB *) (void *) ab; (B *) (AB *) (void *) ab; > Yet if qsort is to sort objects of type AB, then this is precicely what > the void* implementation of compar would be asked to do! No, that is only true if you are using a compar function intended for sorting As or Bs. If it is expecting ABs then it will work fine. -- Barry Margolin, Thinking Machines Corp. barmar@think.com {uunet,harvard}!think!barmar