Sign in to follow this  

Pointers to member functions

This topic is 4866 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a class : class a { int myfunc(void); } myfunc is never implemented. I then want to set my func to point at a function from a dll : &a::myfunc = GetProcAddress(m_hInstance, "newfunc"); I keep getting errors. How do i do it?

Share this post


Link to post
Share on other sites
A member function pointer is not an address. Therefore, you cannot get it via GetProcAddress(), nor use them with any other function that manipulates 'ordinary' pointers.

Share this post


Link to post
Share on other sites
GetProcAddress() is getting the address i want to assign to the function pointer.

The article is not really what I need because it is assigning memeber funtion pointers within the same class which throws up fewer problems. I want to make a member function point to a function in a dll.

Ideas?

Share this post


Link to post
Share on other sites
This may not totally apply, but I was having an issue with my application setting pointers to functions of classes outside of the class, but contained within the same application. The solution was to make the class's fuction that I wanted to point to static, the other workaround was to create a wrapper function to accomplish this, but I took option A.

Try searching through this site to see if it has what you're looking for, I'd read through it myself and try to find it but I'm at work and time is a little limited when it comes to posts, but if this site hasn't got it I wouldn't even know where to begin looking ^_^.

Function Pointers .org

Permafried-

Share this post


Link to post
Share on other sites
First off, as implied by ApochPiQ, &a::myfunc is not an lvalue. You can't assign to it.

Secondly, even assuming you had an actual assignable variable of type pointer to member function, the entity exported by a DLL via GetProcAddress() is, in general, incompatible with the binary layout of C++ member function pointers, which means that it's impossible to create a member function pointer from the result returned by GetProcAddress() without some funked out extra-linguistic techniques, which invariably turn out to be both fragile and extremely compiler specific.

Whatever you are trying to do can probably be accomplished by traditional DLL class exporting techniques i.e. __declspec(dllexport) applied to the class, or creating factory functions in the DLL that return concrete instances of abstract base classes.

Share this post


Link to post
Share on other sites
The way I understand it is that jonbell wants to have a function point in class A which points to a DLL function and which mimics a member function of A (this is essentially what is implied by Stoffel's comment).

If i'm true then this is easily done with a classic C function pointer BUT you will not be able to use this in your DLL function.

so :


class A
{
public:
typedef int (*pfnmyfunc)();

pfnmyfunc myfunc;

A(HANDLE hmodule)
{
myfunc = GetProcAddress(hmodule, "newfunc");
}
};
// ...
A a;
a.myfunc();


Share this post


Link to post
Share on other sites

This topic is 4866 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this