Article View: gnu.g++.help
Article #741Re: g++ and qsort
From: Pete Grant
Date: Wed, 23 Aug 1995 00:14
Date: Wed, 23 Aug 1995 00:14
35 lines
1595 bytes
1595 bytes
In article <DDpG3H.88M@watserv3.uwaterloo.ca> amgaudra@ccnga.uwaterloo.ca (Alain M. Gaudrault) writes:
>
> I'm having a tough time using the C qsort function within a C++
>class. I have the following array of pointers:
>
> static dataItemType** dataBlockVectorA = NULL;
>
>(which I initialize before the call to qsort, obviously), then further on
>down, I call:
>
> qsort( dataBlockVectorA, dataBlockSize, sizeof( dataItemType*), pcompfunc);
>
>where pcompfunc is defined as follows:
>
> int transformDataC::pcompfunc( dataItemType** item1, dataItemType** item2);
>
>As can be seen, the ordering function required by qsort is a member function,
Ok, it's a member function. Now, think about this: On what object
is this function to operate? Remember, member functions are invoked
by either ptr->memberfunc() or obj.memberfunc().
The function specification for qsort requires two void * arguments. The
way to comply with the spec is to do something like:
(Warning! Untested code.)
int pcompfunc (const void * a, const void * b)
{
dataItemType * p1 = (dataItemType*)*a;
dataItemType * p2 = (dataItemType*)*b;
return p1->comparefunction(p2);
}
Pete.
Technical Details
Message-ID:
<DDqM06.52K@pentagon-ai.army.mil>Path:
arc1-ca.pugleaf.net!in2.uu.net!psinntp!psinntp!psinntp!psinntp!hqda-ai!grantReferences:
<DDpG3H.88M@watserv3.uwaterloo.ca>Show Full Headers
Complete Article Headers
Newsgroups: gnu.g++.help,comp.lang.c++ Path: arc1-ca.pugleaf.net!in2.uu.net!psinntp!psinntp!psinntp!psinntp!hqda-ai!grant From: grant@pentagon-ai.army.mil (Pete Grant) Subject: Re: g++ and qsort Message-ID: <DDqM06.52K@pentagon-ai.army.mil> Organization: U.S. Army Artificial Intelligence Center, The Pentagon References: <DDpG3H.88M@watserv3.uwaterloo.ca> Date: Wed, 23 Aug 1995 00:14:30 GMT