Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Sorting a CPtrArray: problems!

Posted on 2008-06-17
4
Medium Priority
?
886 Views
Last Modified: 2013-11-20
Ah hello.

I am trying to sort a CPtrArray of my own simple class.  This is based on the MSDN article at http://support.microsoft.com/kb/q216858/.  Code follows:

typedef int (__cdecl *GENERICCOMPAREFN)(const void * elem1, const void * elem2);
typedef int (__cdecl *STRINGCOMPAREFN)(const CMyClass * elem1, const CMyClass * elem2);

class CMyArray : public CPtrArray
{
public:
      void Sort(GENERICCOMPAREFN pfnCompare = Compare)
      {
            qsort ( this, GetSize(), sizeof ( CMyClass ), ( GENERICCOMPAREFN ) pfnCompare );
      }
protected:

      static int __cdecl Compare(const void * pMyClass1, const void * pMyClass2 )
      {
            if ( !pMyClass1 || !pMyClass2 ) return 0;
            CMyClass* pActualMyClass1 = (CMyClass*)pMyClass1;
            CMyClass* pActualMyClass2 = (CMyClass*)pMyClass2;

            return pActualMyClass1 ->m_dw < pActualMyClass2 ->m_dw;
      }
};

int main()
{
      CMyArray m_array;

      m_array.Add ( ( void*) new CMyClass ( 1 ) );
      m_array.Add ( ( void*) new CMyClass ( 3 ) );
      m_array.Add ( ( void*) new CMyClass ( 2 ) );

      m_array.Sort();

      return 0;
}

I would like to sort my array by the CMyClass object's DWORD member, in ascending order.

I am finding however that when Compare gets called, the DWORD value of the CMyClass object is like over 3386696!  Not what I initialised the object with when I added it to the array!  

Can someone explain what I am doing wrong here, and hence how to sort this array?
0
Comment
Question by:mrwad99
  • 2
4 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 252 total points
ID: 21804349
IIRC that should be

            CMyClass* pActualMyClass1 = *(CMyClass**)pMyClass1;
            CMyClass* pActualMyClass2 = *(CMyClass**)pMyClass2;

since the compare function gets called with an additional indirection.
0
 
LVL 19

Assisted Solution

by:alb66
alb66 earned 248 total points
ID: 21804499
You pass "this" instead of GetData();


      void Sort(GENERICCOMPAREFN pfnCompare = Compare)
      {
            void* p = GetData();
            if ( p )      
                qsort ( p, GetSize(), sizeof ( CMyClass ), ( GENERICCOMPAREFN ) pfnCompare );
      }
0
 
LVL 19

Author Comment

by:mrwad99
ID: 21811133
Both of those changes were required for it to work, so points split!

Thanks both :o)
0
 
LVL 19

Author Comment

by:mrwad99
ID: 21813449
Follow up at http:Q_23495395.html if anyone can help :)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question