Passing a reference of "this" ?
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.
Code:
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.
Re: Passing a reference of "this" ?
Quote:
Originally posted by jase jennings
Hi,...
Code:
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.
Code:
#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
Re: Re: Passing a reference of "this" ?
Paul,
What sort algorithm does this use ?