Solved

Sorting a CPtrArray: problems!

Posted on 2008-06-17
4
836 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 63 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 62 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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
EvenOdd challenge 10 108
SNMP error No Such Object available on this agent at this OID 3 248
Fibonacci challenge 11 121
Can not remove SSL certificate on iPhone 6 - iOS10.2 12 551
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

770 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