Date: Sat, 3 Dec 1994 8:29:25 -0500 (EST) From: Chris Tate To: djgpp AT sun DOT soe DOT clarkson DOT edu Subject: Re: Pointers to Functions in c++ David Wu wrote: > I was wondering if anyone knew a good method to pass member functions > to something. > > if I want to develop a generic list of pointers to functions, that say > draw_me(); > is there any way to do this? > What I am doing know is this, > the list stores two things, the address of the of member (a s a void) > and an integer identifier. > e.g. to call, > > switch (ID) > case BOX : ( (box *)dummyPointer)->draw(); break; > case CIRCLE : ( (circle *)dummyPointer)->draw(); break; > etc. Don't take this wrong, but you need to understand C++ a little better; there's a *far* superior way of doing this sort of thing. In fact, C++ will do all of the bookkeeping for you. If you have declared BOX, CIRCLE, etc. to be subclasses of some base class (let's call it "Shape"), then you can arrange for the ->draw() method call to automagically do the right kind of drawing based on what sort of object is at the other end of your pointer. Here's a sketchy example: class Shape { public: virtual void Draw(void); // must be virtual! }; class BOX : public Shape // a BOX is a kind of Shape { public: void Draw(void); // override Shape::Draw() method }; class CIRCLE : public Shape // a CIRCLE is another kind of Shape { public: void Draw(void); }; (I've left out the other details of the classes; bear with me.) Now, in your code where you want to draw whatever shape it is that you have a pointer to, you'd just use something like this: void DrawAShape(Shape *theShape) { theShape->Draw(); } You don't have to "figure out" what kind of object the Shape pointer is pointed to; the compiler does that for you at runtime. I'd recommend that you (or anyone dealing with C++, actually) get hold of the C++ FAQ that is periodically posted on the USENET group comp.lang.c++. You can FTP the FAQs for any USENET group from the site rtfm.mit.edu, in the /pub/usenet directory hierarchy (I believe). That site also has a number of mirrors around the world; pick one suitable to your location. > I have heard that C++ functions are just liked dos functions in which the > first parameter is "this" is this always the case? Is there a way to > take advantage of this? #define SOAPBOX // :-) This isn't strictly true; in fact, you should *never* cast object or method pointers to "void *" and back, because you may well break the virtual function mechanism if you're not highly aware of what is going on. Method pointers are not the same as function pointers, and in fact C++ treats them as different types. Trying to get around this type-safety is a good sign that your design may be poor. From what you've said, I'd go with object pointers rather than method pointers, and rely on the virtual method dispatch mechanism to Do The Right Thing - that's what it was designed for. #undef SOAPBOX -------------------------------------------------------------------- Christopher Tate | "Apple Guide makes Windows' help engine fixer AT faxcsl DOT dcrt DOT nih DOT gov | look like a quadruple amputee." eWorld: cTate | -- Pete Gontier (gurgle AT dnai DOT com)