Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!clyde.concordia.ca!uunet!nuchat!moray!urchin!f506.n106.z1.fidonet.org!Roy.Browning From: Roy.Browning@f506.n106.z1.fidonet.org (Roy Browning) Newsgroups: comp.lang.c++ Subject: Passing function pointers in C++ Message-ID: <5115.25805AED@urchin.fidonet.org> Date: 8 Dec 89 19:04:45 GMT Sender: ufgate@urchin.fidonet.org (newsout1.26) Organization: FidoNet node 1:106/506 - Fulcrum's Edge, Spring TX Lines: 73 > From: abbott@aerospace.aero.org (Russell J. Abbott) > Date: 1 Dec 89 16:30:44 GMT > Organization: The Aerospace Corporation, El Segundo, CA > Message-ID: <62298@aerospace.AERO.ORG> > Newsgroups: comp.object,comp.lang.c++ > > In article <32106@watmath.waterloo.edu> gjditchfield@watmsg.waterloo.edu > (Glen Ditchfield) writes: > > ... In practice, in C++, wouldn't it be simpler to pass a comparison > >function as an argument to the sort() function? > > Can you really do that in C++? What would the declarations look like? > That would be even more flexible than the generic sort that I asked > about originally. Russell: Yes you can pass function pointers in C++, an edited version of the syntax is listed below. Also if you wish to look at some very simple code call my system and download DL_2.ZIP. Personally I believe that deriving different sorting methods from the base class is much cleaner and more in line with OOP. Migrating from C to C++, Roy Browning HEADER extern "C" { typedef int (*CMP)(const void **,const void **); extern int Cmp_FileName(const struct FIND **p1, const struct FIND **p2); .... .... }; class FileList { ....; ....; public: FileList(const char *name = "*.*", const int attrib = zero, CMP func = NULL); ....; ....; }; DEFINITION static FileList::FileList(const char *name, const int attrib, CMP func) { ....; ....; if (func) qsort(F,num_files,sizeof(struct FIND *), func); } TEST main() { FileList test5("*.*",FA_DIREC | FA_HIDDEN, Cmp_FileSize); ....; ....; }