Path: utzoo!attcan!uunet!decwrl!fernwood!dumbcat!marc From: marc@dumbcat.sf.ca.us (Marco S Hyman) Newsgroups: comp.lang.c++ Subject: Re: Address of member function Summary: The worrkaround is not to bad Message-ID: <220@dumbcat.sf.ca.us> Date: 29 Oct 90 01:00:29 GMT References: <1399@carol.fwi.uva.nl> Organization: MH Software, Hayward, Ca. Lines: 77 In article <1399@carol.fwi.uva.nl> delft@fwi.uva.nl (Andre van Delft) writes: A few days ago I had a question on comp.lang.c++ about taking the address of a member function: [[...]] I meant the following: why is it allowed to take the address of: a GLOBAL VARIABLE a GLOBAL FUNCTION a MEMBER VARIABLE, i.e. a variable local to an instance of a class but why is it NOT allowed to take the address of a MEMBER FUNCTION, i.e. a function local to an instance of a class I think the problem here is that member functions are not "local to an instance of a class," but are part of the class itself. Given the class class c { public: int j; int g(int k) {printf("%d\n",j+k);} }; c aC; there is a j for every instance, therefore you can take the address of aC.j or you can get its relative offset in the class by taking the address of c::j. There is only one g for the class used by all instances and you can only get its offset (&c::g). See section 8.1c of the ARM (pg 155). Note: by making minor changes to your example you can get what you want, albeit by special casing pointers to member functions. #include int i; int f(int k) {printf("%d\n",i+k);return 0;} // return 0 added to get rid of warning class c { public: int j; int g(int k) {printf("%d\n",j+k); return 0;} // return 0 added to get rid of warning }; main() { int*ip,*jp; int(*fp)(int); //int(*gp)(int); // replaced gp with the next two lines int (c::*gp)(int); c * gpObject; c anObject; ip = & i; // OK jp = &anObject.j; // OK fp = & f; // OK //gp = &anObject.g; // NOT OK but see the next two lines gpObject = &anObject; gp = &c::g; *ip = 1; *jp = 2; fp(3); // prints 4 //gp(4); // would print 6, if not NOT OK ... (gpObject->*gp)(4); // this is OK and prints 6 with CC 2.0 } It's more work than you want to do but gets the job done. // marc -- // marc@dumbcat.sf.ca.us // {ames,decwrl,sun}!pacbell!dumbcat!marc