CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2001
    Location
    The Netherlands
    Posts
    100

    Pointer to an CallBack

    Is it posible to make a sort function with an pointer to an callback?
    PHP Code:
    //...
    void Sort(LPCSTR *CallBackName// This can't compile....!!
    {
            
    qsort(this->GetData(), this->GetSize(), sizeof(ARG_T), CallBackName);
    //... 
    /* Regards, David Smulders */

  2. #2
    Join Date
    Jun 2002
    Posts
    395
    The prototype for qsort is

    Code:
    void qsort(
       void *base,
       size_t num,
       size_t width,
       int (__cdecl *compare )(const void *, const void *) 
    );
    So the function you want must have the same prototype as the compare function:

    Code:
    int compare( const void *arg1, const void *arg2 )
    {
         ARG_T *argT_1 = (ARG_T *)arg1;
         ARG_T *argT_2 = (ARG_T *)arg2;
    
        if (argT_1 > argT_2 ) return 1;
        else if (argT_1 < argT_2) return -1;
        else return 0;
    }

    void Sort(int (__cdecl *CallBackName)(const void *, const void *) ) {

    qsort(this->GetData(), this->GetSize(), sizeof(ARG_T), CallBackName);

    }

  3. #3
    Join Date
    Jan 2001
    Location
    The Netherlands
    Posts
    100
    Thanks!!!
    /* Regards, David Smulders */

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured