By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,234 Members | 1,872 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,234 IT Pros & Developers. It's quick & easy.

Pointing to non-static member functions

P: 2
Hi All,

The code that I am trying to write requires that a function pointer be made to point to a non-static member function of a class object. More specifically,

class ExampleClass{
private:
double member_function(int);
}

function pointer declaration somewhere else;
double (*func_pointer)(int)

What I want to do
func_pointer = member_function;

I thought I may be able to get away with using reinterpret_cast but I am not too sure how to, or if what I am asking for is even allowed.
I have tried to think of a way around this 'recasting', but it would be great if the above can be done. Any help will be most appreciated.

cheers,

Stargazer
Mar 13 '07 #1
Share this Question
Share on Google+
2 Replies


sicarie
Expert Mod 2.5K+
P: 4,677
Hi All,

The code that I am trying to write requires that a function pointer be made to point to a non-static member function of a class object. More specifically,

class ExampleClass{
private:
double member_function(int);
}

function pointer declaration somewhere else;
double (*func_pointer)(int)

What I want to do
func_pointer = member_function;

I thought I may be able to get away with using reinterpret_cast but I am not too sure how to, or if what I am asking for is even allowed.
I have tried to think of a way around this 'recasting', but it would be great if the above can be done. Any help will be most appreciated.

cheers,

Stargazer
Right now func_pointer is not delcared in the class, and memeber_function is private, so I don't believe you will be able to do that, you'll need some sort of accessor method (getMemberFunction() or something) to return the value. Also, several compilers will cast an into to a double automatically (for instace 5/3.6 - the 5 can be converted to 5.0), but you have to watch out where you assign that to, it's possible to lose the value (or break the program).
Mar 13 '07 #2

P: 2
Right now func_pointer is not delcared in the class, and memeber_function is private, so I don't believe you will be able to do that, you'll need some sort of accessor method (getMemberFunction() or something) to return the value. Also, several compilers will cast an into to a double automatically (for instace 5/3.6 - the 5 can be converted to 5.0), but you have to watch out where you assign that to, it's possible to lose the value (or break the program).

Thanks Sicarie for replying....

Silly me I made a mistake in the above example. The member_function is actually public in scope.... I just need to have a generic function pointer, that knows nothing of classes, to point to a class member function of the same signature.
Mar 14 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.