dave@andromeda.rutgers.edu (Dave Bloom) (02/08/89)
Hi there. We're running AT&T's C++ Ver 1.2.1, and as far as I'm concerned, the following should work hunky-dorey. Does anybody see anything wrong with my syntax? ---------------------------------------------------------------------- class zz { public: void (*xx)(char*); void a(char* s) { printf("function a: %s\n", s); } void b(char* s) { printf("function b: %s\n", s); } void c(char* s) { printf("function c: %s\n", s); } zz(int i) { if(i==0) xx= &(zz::a); else if(i==1) xx= &(zz::b); else xx= &(zz::c); } }; main() { zz dave(1); dave.xx("Yes!!!"); } ---------------------------------------------------------------------- Thanx. _______________________________________________________________________________ rutgers\ | Dave Bloom galaxy >!andromeda!dave -or- dave@andromeda.rutgers.edu | Work: (201)648-5085 pyramid/ |
dean@homxb.ATT.COM (D.JONES) (02/08/89)
In article <2205@galaxy>, dave@andromeda.rutgers.edu (Dave Bloom) writes: > Hi there. We're running AT&T's C++ Ver 1.2.1, and as far as I'm > concerned, the following should work hunky-dorey. Does anybody > see anything wrong with my syntax? > > class zz { > public: > void (*xx)(char*); > void b(char* s) { printf("function b: %s\n", s); } > zz(int i) { > if(i==0) xx= &(zz::a); > else if(i==1) xx= &(zz::b); > else xx= &(zz::c); > } > }; > main() > { > zz dave(1); > dave.xx("Yes!!!"); > } > rutgers\ | Dave Bloom > galaxy >!andromeda!dave -or- dave@andromeda.rutgers.edu | Work: (201)648-5085 > pyramid/ | Dave, The problem seems to be that the compiler does not recognize xx as a `member function', where a,b, & c are member functions. Since they are member functions they expect a pointer to the object or a ``this'' pointer as the first argument. If you call them indirectly through xx the ``this'' pointer is not added to the stack of xx, it pulls off "Yes!!!" as ``this'' and as for where the second argument, or s points, all bets are off ..... Dean S Jones UNICAD/IDS AT&T Bell Labs Holmdel, NJ dean@homxb.ATT.COM Line.Eater() Line.Eater() Line.Eater() Line.Eater() Line.Eater() Line.Eater() Line.Eater() Line.Eater() Line.Eater()
tom@tnosoes.UUCP (Tom Vijlbrief) (02/11/89)
dave@andromeda.rutgers.edu (Dave Bloom) writes: >Hi there. We're running AT&T's C++ Ver 1.2.1, and as far as I'm >concerned, the following should work hunky-dorey. Does anybody >see anything wrong with my syntax? Yes, gnu g++ 1.32 reports: t.c:8: assignment between incompatible pointer types t.c:9: assignment between incompatible pointer types t.c:10: assignment between incompatible pointer types This can be corrected: ====================== class zz { public: void (zz::* xx)(char *); void a(char* s) { printf("function a: %s\n", s); } void b(char* s) { printf("function b: %s\n", s); } void c(char* s) { printf("function c: %s\n", s); } zz(int i) { if(i==0) xx= &(zz::a); else if(i==1) xx= &(zz::b); else xx= &(zz::c); } }; main() { zz dave(1); dave.xx("Yes!!!"); } ========== This produces: t.c:17: invalid call via pointer-to-member function Changing the code to: class zz { public: void (zz::* xx)(char *); void a(char* s) { printf("function a: %s\n", s); } void b(char* s) { printf("function b: %s\n", s); } void c(char* s) { printf("function c: %s\n", s); } void e() { xx(this, "yes"); } zz(int i) { if(i==0) xx= &(zz::a); else if(i==1) xx= &(zz::b); else xx= &(zz::c); } }; main() { zz dave(1); dave.e(); } ====== produces the correct result. Tom =============================================================================== Tom Vijlbrief TNO Institute for Perception P.O. Box 23 Phone: +31 34 63 62 77 3769 ZG Soesterberg E-mail: tnosoes!tom@mcvax.cwi.nl The Netherlands or: uunet!mcvax!tnosoes!tom ===============================================================================