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]

C++ function pointers and automatic class refrences


Hi all,

First off, I'm not sure if this is a good place to send this message
to.  So if you know somewhere better please tell me. . .

Well what I'm trying to do is get something like C function pointers
but with a reference to an object included so I don't have to use
static functions. e.g.

#include <iostream>

class Foo
{
	int _i;

public:
	Foo() {
		_i = 10;
	}

	void bar(int i) {
		cout << i * _i << endl;
	}
};

void baz(void (*fn)(int))
{
	fn(10);
}

int main()
{
	class Foo f;

	baz(f.bar);
}

I know this as it stands is impossible, but what I'm trying to do is
tell the compiler to make the 'fn' parameter of 'baz' automatically
take a reference of the object and maintain it with the function
pointer.  This would allow the function to be called with complete
type safely as far as I can tell, but I could be wrong. . .  Is there
any way of doing this? or am I going to have to resort to using static
functions; its not very elegant using them but at least I know it's
possible.

If you need any more info, please feel free to ask. . . and thanks in
advance for the help.

Thanks,
  Sam Mason


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