Roy.Browning@f506.n106.z1.fidonet.org (Roy Browning) (12/09/89)
> 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); ....; ....; }
fst@gtenmc.UUCP (Fariborz "Skip" Tavakkolian) (12/11/89)
In article <5115.25805AED@urchin.fidonet.org> Roy.Browning@f506.n106.z1.fidonet.org (Roy Browning) writes: > > From: abbott@aerospace.aero.org (Russell J. Abbott) > > In article <32106@watmath.waterloo.edu> gjditchfield@watmsg.waterloo.edu > > > ... 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? [deleted] >Personally I believe that deriving different sorting methods from the base >class is much cleaner and more in line with OOP. I have a similar question, or it may be a major confusion in my part. Say I have a class like this: class genericField { protected: int length, line, column; // here I want something similar to the following three int (genericField::*before)(); int (genericField::*verify)(); int (genericField::*after)(); public: genericField(int len, int ln, int col); ~genericField(); virtual int edit(cursesWindow& w); void setBefore(int (genericField::*user_before_function)()); ... }; class dateField : public genericField { ... }; class integerField : public genericField { ... }; // and so on ... Generally speaking the ``before'' is to be invoked just before editing is attempted. For example suppose we have integerField customer_id; integerField inventory_code; The ``before'' function for customer_id may make a menu of all allowed customer ids, and the ``before'' function for inventory_code will make a list of valid inventory items (from a database for example). As can be demonstrated each instance of the integerField class may need a different ``before'' function. Question: how to do this so that a programmer will not need to derive a new class for each occurrence of a field (i.e. not using virtuals)? Many thanks Skip -- ---------------------------------------------------------------------------- Fariborz "Skip" Tavakkolian -of- Automated Cellular Engineering Currently consulting -at- GTE Telecom, Inc. Bothell, Wa Mail: tiny1!fst@mcgp1 -or- fst@gtenmc