From: authentic AT tip DOT nl (Rick) Newsgroups: comp.lang.c++,comp.os.msdos.djgpp Subject: Re: list.sort(const void*(*fnc)()) again;=) Date: Wed, 15 Apr 1998 22:31:04 GMT Organization: NL-NIC Lines: 237 Message-ID: <6h60uh$5ko$1@cadmium.aware.nl> References: <6gsv3d$dli$1 AT cadmium DOT aware DOT nl> <3533f431 DOT 6208206 AT news DOT ist DOT utl DOT pt> Reply-To: authentic AT tip DOT nl NNTP-Posting-Host: nijmegen-124.trb.pop.tip.nl To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Precedence: bulk deepblack AT geocities DOT com (Luís Coelho) wrote: Luis thank you for your reactions. Thanks to you, and also to Oleg.. As another non-standard reaction I will give the solution.. . Hope you can appreciate it..and hope that visual c would compile it too(tested on djgpp..) Rick #include #include #include class datab { public: friend ostream& operator<<(ostream&, const datab&); datab(char* _name='\0',int _id=0,char* _tel='\0') :name(string(_name)),id(_id),telef(string(_tel)){}; friend int operator<(datab&,datab&); const string get_name()const{return name;} const string get_telef()const{return telef;} int get_id()const {return id;} private: const string name; const string telef; int id; }; ostream& operator<<(ostream& os, const datab& _datab) { os<<_datab.name<<'\t' <<_datab.id<<'\t' <<_datab.telef< class sorted_list:public list { public: //overladen van ingebouwde sort: dat is alles void string_sort(const string (T::*get_function)()const); //Om dat te doen moet je dus eigenlijk de call //de merge overladen(die gebruikt operator < en !=) void string_merge(sorted_list&,const string (T::*get_function)()const); }; //Merge&Sort Code below is merely a cut and paste thing from //the gcc sources(with minor adjustments). Thanks again. //A great source of info for me.. template void sorted_list::string_sort (const string (T::*get_function)()const) { if (size() < 2) return; sorted_list carry; sorted_list counter[64]; int fill = 0; while (!empty()) { carry.splice(carry.begin(), *this, begin()); int i = 0; while(i < fill && !counter[i].empty()) { counter[i].string_merge(carry,get_function); carry.swap(counter[i++]); } carry.swap(counter[i]); if (i == fill) ++fill; } for (int i = 1; i < fill; ++i) counter[i].string_merge(counter[i-1],get_function); swap(counter[fill-1]); } template void sorted_list::string_merge (sorted_list& x,const string (T::*get_function)()const) { iterator first1 = begin(); iterator last1 = end(); iterator first2 = x.begin(); iterator last2 = x.end(); while (first1 != last1 && first2 != last2) if (((*first2).*get_function)() < ((*first1).*get_function)()) { iterator next = first2; transfer(first1, first2, ++next); first2 = next; } else ++first1; if (first2 != last2) transfer(last1, first2, last2); length += x.length; x.length= 0; } int main() { sorted_list lijst1; ostream_iteratorouts1(cout); sorted_list::iterator i; const string (datab::*string_fnc)()const; listlijst2; ostream_iteratorouts2(cout); datab temp("Annet",1,"743"); lijst1.push_front(temp); lijst1.push_front(datab("Patty",2,"456")); lijst1.push_front(datab("Han",3,"123")); lijst1.push_front(datab("Harry",4,"741")); //This calls the stl inhereted sort string_fnc=&datab::get_name; i=lijst1.begin(); cout<<(*i).get_name()<On Sun, 12 Apr 1998 12:04:31 GMT, authentic AT tip DOT nl (Rick) uttered >the following words: >>Dear c++ programmers, Question 1 answered by a few very nice people. I hope all compilers complie;=) >bool operator < (myclass e11, myclass e12);? >^^^^ Probably >const void* (myclass::*get_fnc) () const = &myclass::get_myname; >I think you always need the & on member functions. Could be >wrong, though. Well it compiled without &, and given the code I think the compiler has only one choice here: give it the adress of the function... But maybe adding & is good for readability;=))) hahahaha Remains: 3. How do I reference that pointer inside my template list ?(the example below) thats the more problematic one, and also the only problem that is between me and my generiek listsort. when i have a child of stl list(like said before: simple) and: myclass temp(.....); mylist lijst1; mylist::iterator i; const void* (myclass::*get_fnc) () const = &myclass::get_myname; //fill list1 i=lijst1.begin; I CAN directly reference a member fnc cout<<(*i).getmyname()<Hope that helped,