richard@ben.Jpl.Nasa.Gov (Conan the Barbarian) (03/20/90)
I am running into a strange problem trying to insert a pointer to a member function in the argument list to a non-member function while running the constructor on an instance of a class. In short the translator gets lost and puts garbage in the call. Sun c++ states that you should use &a::proc for the address of member function proc in class a. This does give the correct value in the debugger but it can not be used in an argument list. I tried moving the address to a temporary pointer. For example if void proc(char*); is the declartation in a, then void (a::*func_ptr)(char*) = &a::proc; should create a pointer to proc which I can insert in the function call proc2(func_ptr); (proc2 is not a member function of a.) The translator returns no errors with the use of the temporary. However, func_ptr is not what it seems. It is not a pointer to proc. It is a "structure" with member f pointing to proc. Except that you may not reference member f. Anybody have any ideas on how to insert the pointer to a member function in a non-member function call inside a constructor for a class? (This is used to register a notifier function in xview. I know c++ and xview don't mix.)
shankar@hpclscu.HP.COM (Shankar Unni) (03/21/90)
> void (a::*func_ptr)(char*) = &a::proc; > ... > proc2(func_ptr); > ... > (proc2 is not a member function of a.) > ... > However, func_ptr is not what it seems. It is not a pointer to > proc. It is a "structure" with member f pointing to proc. Except > that you may not reference member f. So what? You don't need the actual address of a::proc in any case. Remember that since "func_ptr" points to a *member* function of class "a", you should only be calling it USING AN OBJECT OF CLASS "a" (or one of its descendants). In this case, you should be using the ->* or .* operator to call the function. #include <stdio.h> class a { public: void proc(char *x) { printf ("a::proc: \"%s\"\n", x); } }; void proc2 (void (a::*p)(char *)) { a *t = new a; (t->*p) ("test"); // Call through pointer-to-member-function } main() { void (a::*func_ptr)(char *) = &a::proc; proc2 (func_ptr); } ----- Shankar Unni E-Mail: Hewlett-Packard California Language Lab. Internet: shankar@hpda.hp.com Phone : (408) 447-5797 UUCP: ...!hplabs!hpda!shankar