Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!samsung!uunet!comp.vuw.ac.nz!actrix!Bruce.Hoult From: Bruce.Hoult@bbs.actrix.gen.nz Newsgroups: comp.lang.c++ Subject: Re: Comparison functions for qsort() and bsearch() Keywords: Sun, C++ Message-ID: <1991Jan5.105957.23014@actrix.gen.nz> Date: 5 Jan 91 10:59:57 GMT References: <277640DD.4A70@tct.uucp> <1990Dec26.160636.15566@clear.com> <1991Jan2.191630.18581@Think.COM> Sender: Bruce.Hoult@actrix.gen.nz (Bruce Hoult) Organization: Actrix Information Exchange, Wellington, New Zealand Lines: 35 Comment-To: barmar@think.com Barry Margolin writes: >Bob Martin writes: >> 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; That doesn't solve the problem in question, since the compare function will most likely know that it is comparing A's or B's, but not that they are embedded in AB's. After all, if you're writing an A or B compare function that *does* know this, then you might as well write a special one for AB's anyway. What *will* work is... (A*) (void*)(A*) ab; or (B*) (void*)(B*) ab; -- Bruce.Hoult@bbs.actrix.gen.nz Twisted pair: +64 4 772 116 BIX: brucehoult Last Resort: PO Box 4145 Wellington, NZ