Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sort() using a member function 1

Status
Not open for further replies.

titanandrews

Programmer
Feb 27, 2003
130
US
Hi,
I am using the sort() function from <algorithm> and trying to use a member function for the sort implementation.
Like this:
Code:
bool MyView::SortAsc(const string& x, const string& y)
{
   //do some sorting
   return true;
}

void MyView::MySortCaller()
{
   vector<string> myVect;
  
   //Put some stuff in them
   sort(myVect.begin(),myVect.end(),SortAsc);
}

If I remove the SortAsc() function from the class, then it works fine i.e. (bool SortAsc(const string& x, const string& y), but this function must be a member function because I HAVE to access member variables. How can I make this function a member and reference it from the sort() function. I have tried
Code:
sort(myVect.begin(),myVect.end(),this->SortAsc); 
sort(myVect.begin(),myVect.end(),SortAsc); 
sort(myVect.begin(),myVect.end(),MyView::SortAsc);

Nothing works. I get compiler errors.

many thanks to you,


Barry



 
Yeah that's not going to work. Technically you can use a member function if it is static, but then of course you won’t have access to members so it does not solve your problem.

You might consider using a Singleton object to access from within the sort function. Also make sure your design makes sense. Generally data other than the objects being sorted is not needed in a sort function.


-pete
 
&quot;Generally data other than the objects being sorted is not needed in a sort function.&quot;

Yes, I agree. But this is a bizarre circumstance, believe me. The Singleton may work. Thank you!


Barry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top