Click to See Complete Forum and Search --> : Passing a reference of "this" ?


jase jennings
February 27th, 2003, 05:37 PM
Hi

Is it possible to pass a reference of "this" ? I am extending the MFC CStringArray class. I need to reference the string array using the [] operator, but i cannot do this on a pointer, it needs to be on a reference to the original object. I don't have access to the actual member data (as far as i can tell from the docs)

Am i making this clear ? Here's a code snippet which will explain.


void SortStringArray (CStringArray &csa, BOOL bDescending)
{
int iArraySize = csa.GetSize();
if (iArraySize <= 0)
return;

int iCSSize = sizeof (CString*);
void* pArrayStart = (void *)&csa[0]; // i cannot do this on a pointer, so i cannot pass a "this" pointer to this func

if (bDescending)
qsort (pArrayStart, iArraySize, iCSSize, CompareDescending);
else
qsort (pArrayStart, iArraySize, iCSSize, CompareAscending);
}

void CSortStringArray::QuickSort(bool *pbCancel /* = false */)
{
SortStringArray(this, FALSE); // can't pass "this". Need to pass a reference ... but how ?
}


Thanks for any suggestions.

Graham
February 27th, 2003, 05:41 PM
void CSortStringArray::QuickSort(bool *pbCancel /* = false */)
{
SortStringArray(*this, FALSE);
}

Paul McKenzie
February 27th, 2003, 05:52 PM
Originally posted by jase jennings
Hi,...


void SortStringArray (CStringArray &csa, BOOL bDescending)
{
int iArraySize = csa.GetSize();
if (iArraySize <= 0)
return;

int iCSSize = sizeof (CString*);
void* pArrayStart = (void *)&csa[0]; // i cannot do this on a pointer, so i cannot pass a "this" pointer to this func

if (bDescending)
qsort (pArrayStart, iArraySize, iCSSize, CompareDescending);
else
qsort (pArrayStart, iArraySize, iCSSize, CompareAscending);
}

void CSortStringArray::QuickSort(bool *pbCancel /* = false */)
{
SortStringArray(this, FALSE); // can't pass "this". Need to pass a reference ... but how ?
}


Thanks for any suggestions. If you wanted to sort a CStringArray, you could have saved yourself a lot of time by just using std::sort.

#include <algorithm>

bool SortDescending( const CString& first, const CString& second)
{
return first > second;
}

void SortStringArray (CStringArray &csa, BOOL bDescending)
{
if ( !bDescending )
std::sort(csa.GetData(), csa.GetData() + csa.GetSize() );
else
std::sort(csa.GetData(), csa.GetData() + csa.GetSize(),
SortDescending);
}

This is also covered in the FAQ here:

http://www.codeguru.com/FAQS/#501

Regards,

Paul McKenzie

jase jennings
February 27th, 2003, 05:57 PM
... of course it is. thanks for the reminder.

jase jennings
February 27th, 2003, 06:07 PM
Paul,

What sort algorithm does this use ?

Bob Davis
February 27th, 2003, 07:38 PM
What sort algorithm does this use ?


I believe this is implementation dependent. If I remember correctly, std::sort uses quicksort, but is type-safe, so as to differentiate it from qsort(). I remember one person linking to an STL implementation that used a different algorithm that I'd never heard of; I think it was written by some folks at SGI. If I remember correctly, it was in the thick of the big qsort() vs. std::sort thread last summer; a search might yield some results.

Philip Nicoletti
February 27th, 2003, 07:54 PM
std::sort() is based on quicksort. It is not guarenteed
that this is the case - the standard puts various complexity
restrictions on its sort algorithms. As an example - heapsort
would be valid to use as nthe algoruthm for std::sort - but
generally heapsort is considerably slower than quicksort.
I would guess that all the current algorithms used by std::sort
are based on quicksort.

Many implementations (including the latest VC++/Dinkumware)
use introsort. Introsort starts as a quicksort, but changes to a
heap sort if the complexity is going to be O(n^2).

and for using qsort() on a CStringArray see ...

http://support.microsoft.com/default.aspx?scid=kb;en-us;216858