Xref: utzoo comp.lang.c++:7801 gnu.g++:864 Path: utzoo!utgpu!watserv1!watmath!uunet!samsung!sol.ctr.columbia.edu!emory!hubcap!rajarar From: rajarar@hubcap.clemson.edu (Bala Rajaraman) Newsgroups: comp.lang.c++,gnu.g++ Subject: Question re. Pointers to functions (Passed Parameters are Garbage) Keywords: Pointers to functions/members, Garbage Parameters Message-ID: <9140@hubcap.clemson.edu> Date: 28 May 90 21:56:30 GMT Distribution: na Organization: Clemson University, Clemson, SC Lines: 66 Hi, I have a question regarding pointers to functions and a weird problem I am facing. I need to call a function (which could either be a global function or a member function of a class). I store a pointer to this function in a private variable in the class. I look at two cases for the initialization of this private variable, 1. A global function 2. A member function of the same class In both cases, the functions have an int parameter. However, when I print this int value, I get a correct answer if the function is global but garbage if the function pointer is that of a member function. I have enclosed a minimal program where this seems to occur. This also occurs in a broader context. The life of a simulation hinges on this. So, HELP!! HELP!! HELP!! HELP!! HELP!! This is rather urgent and I have really tried a lot of variations to crack this puzzle. THANKS, Bala Rajaraman ---------------------------- CUT HERE ------------------------------ #include typedef int (*intfn)(int); class y { intfn myfn; public: abc() { myfn = intfn(def); (*myfn)(2); } abc(intfn a) { myfn = a; (*myfn)(2); } def(int a) { cout << "DEF: " << a << "\n"; } }; fin(int a) { cout << "FIN: " << a << "\n"; } main() { y ysp; ysp.abc(fin); ysp.abc(); intfn d = intfn(&y::def); ysp.abc(d); } -------------------------------------------------------------------------- COMPILATION and RESULT: bengal[32%] g++ -o x x.c bengal[33%] x FIN: 2 DEF: 8848 DEF: 8848 bengal[34%]