This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: C++ function pointers and automatic class refrences


Sorry to respond to my own question, but I've written a throughly EVIL
set of macros to do what I want.  They aren't type safe or anything
and there is an unnecessary typedef to get around a warning, but the
below works now. . .

On Wed, May 22, 2002 at 09:56:39AM +0100, Mason, Sam wrote:
>#include <iostream>
>
>class Foo
>{
>	int _i;
>
>public:
>	Foo() {
>		_i = 10;
>	}
>
>	void bar(int i) {
>		cout << i * _i << endl;
>	}
>};

#define CFP_DECLARE(name, ret)   void * name##_p, ret(*name##_fn)(void*, ...)
#define CFP_CALL(name, args)     name##_fn(name##_p, args)
#define CFP(class, method)       (class), (void(*)(void *, ...))(int)((class)->*&method)

void baz(CFP_DECLARE(fn, void))
{
	CFP_CALL(fn, 10);
}

int main()
{
	class Foo f;

	baz(CFP(&f, Foo::bar));
}

I hope you agree it's pretty grim, but it does work.  I think it could
be exploiting a bug in GCC (I'm using version 2.96, RH7.2 standard),
but I'm not sure.  It would be nice to get rid of the second type cast
in the CFP macro to make it like:

#define CFP(class, method) (class), (void(*)(void *, ...))((class)->*&method)

The problem is that it won't compile without it.  Is this sort of
thing possible with C++, or is this sort of thing impossible. Anyway,
hope I'm not chatting away to my self here and somebody is finding
this interesting.

Thanks for your time,
  Sam.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]