rajarar@hubcap.clemson.edu (Bala Rajaraman) (05/29/90)
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 <stream.h>
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%]