CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Mar 2000
    Location
    Birmingham, England
    Posts
    2,520

    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.

    Jase

    www.slideshowdesktop.com
    View your images and photos on your desktop with ease using SlideShow Desktop, the desktop wallpaper manager for Microsoft Windows.
    ...

  2. #2
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    Code:
    void CSortStringArray::QuickSort(bool *pbCancel /* = false */)
    {
        SortStringArray(*this, FALSE); 
    }
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Passing a reference of "this" ?

    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

  4. #4
    Join Date
    Mar 2000
    Location
    Birmingham, England
    Posts
    2,520
    ... of course it is. thanks for the reminder.

    Jase

    www.slideshowdesktop.com
    View your images and photos on your desktop with ease using SlideShow Desktop, the desktop wallpaper manager for Microsoft Windows.
    ...

  5. #5
    Join Date
    Mar 2000
    Location
    Birmingham, England
    Posts
    2,520

    Re: Re: Passing a reference of "this" ?

    Paul,

    What sort algorithm does this use ?

    Jase

    www.slideshowdesktop.com
    View your images and photos on your desktop with ease using SlideShow Desktop, the desktop wallpaper manager for Microsoft Windows.
    ...

  6. #6
    Join Date
    Jan 2001
    Posts
    588
    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.

  7. #7
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725
    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...b;en-us;216858
    Last edited by Philip Nicoletti; February 27th, 2003 at 11:03 PM.

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