Proper way to cast function pointers.
Proper way to cast function pointers.
- Subject: Proper way to cast function pointers.
- From: Steve Checkoway <email@hidden>
- Date: Thu, 12 Oct 2006 01:55:10 -0700
I'm not positive that this is the best list for this since it's more
of a c++ issue but it is related to apple's g++ and so forth. If
there's a more appropriate list (such as darwin-dev), I'll go there.
I'm looking for the proper way to cast a function pointer in c++. In
particular an OpenGL entry point. Writing code similar to <http://
developer.apple.com/qa/qa2001/qa1188.html> (which uses deprecated
functions), I have something like the following:
#include <mach-o/dyld.h>
#include <cstdlib>
#include <cstdio>
#include <inttypes.h>
#include <string>
using namespace std;
static void *NSGLGetProcAddress( const string &name )
{
NSSymbol symbol = NULL;
string symbolName( '_' + name );
const uint32_t count = _dyld_image_count();
const uint32_t options =
NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR;
for( uint32_t i = 0; i < count && !symbol; ++i )
symbol = NSLookupSymbolInImage
( _dyld_get_image_header(i), symbolName.c_str(), options );
return symbol ? NSAddressOfSymbol( symbol ) : NULL;
}
typedef void (*glFunc)();
int main()
{
void *addr = NSGLGetProcAddress( "glBlendEquation" );
glFunc glBlendEquation = reinterpret_cast<glFunc>( addr );
return !glBlendEquation;
}
The glFunc typedef is supposed to be just any generic function (is
there a better way to do this such as void (*)(...) perhaps?). The
problem is that NSAddressOfSymbol returns a void * which is not a
function pointer. When I compile, I get:
$ g++ e.cc -Wall -pedantic -framework OpenGL
e.cc: In function ‘int main()’:
e.cc:26: warning: ISO C++ forbids casting between pointer-to-function
and pointer-to-object
This makes sense given that a function pointer need not even be the
same size as an object pointer. Is there a correct way to do this cast?
--
Steve Checkoway
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden