Re: Proper way to cast function pointers.
Re: Proper way to cast function pointers.
- Subject: Re: Proper way to cast function pointers.
- From: Jesper Papmehl <email@hidden>
- Date: Thu, 12 Oct 2006 17:43:38 +0200
I don't know if it is the "correct" way, but I have managed to get
rid of the warning by first casting pointers-to-objects to some handy
integer type (like uintptr_t), and then to a pointer-to-function. (Or
the other way around if you need to go in that direction.)
For example:
TFunctionPointerType functionPointer =
reinterpret_cast<TFunctionPointerType>
(reinterpret_cast<std::uintptr_t>(objectPointer));
(typed directly into e-mail client, not tested)
HTH
/Jesper
12 okt 2006 kl. 10.55 skrev Steve Checkoway:
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
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
40propellerheads.se
This email sent to email@hidden
_______________________________________________
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