Skip to main content
gamedev.net
🔒 Locked

Short Question on Function Pointers

Started by Kincaid Jun 2, 2006 at 4:07 AM 2 replies 750+ views
Original Post
Kincaid
Kincaid
Hello I know how to use standard function pointers in C++. eg; float (*funcpointer1)(arg1, arg2); void (*funcpointer2)(arg1, arg2); but how to declare other non-primitive type like return type of self-defined structure or CALLBACK (which I need) struct Vector {...}; Vector (*funcpointer2)(arg1, arg2); // doesn't work CALLBACK (*funcpointer1)(arg1, arg2); // doesn't work Can anyone tell me how to use CALLBACK function pointers (or any other return-type which isn't standard void, float etc )
Kincaid
Kincaid
I would rather not be working with additional includes.
Can't it be fixed without some external library ?


moeron
moeron
What compiler are you using? One thing I could see as being a problem about a struct named CALLBACK is that some compilers define CALLBACK to be __stdcall, or some other function calling convention.

The following code works fine for me
#include <iostream>struct foo{	int a;};foo (*func1)( int );foo myFunc( int a ){	foo my_foo;	my_foo.a = a;	return( my_foo );	}int main(int,char**){	func1 = myFunc;	foo my_foo = (*myFunc ) ( 24 );	std::cout << my_foo.a << std::endl;	return 0;}

Perhaps you could tell us what compiler errors are preventing that code from working for you.

[edit]
Ok, upon re-reading your post I realized you are trying to use the CALLBACK calling convention...here is how I would have to redefine my functions for that
#include <iostream>struct foo{	int a;};// I don't remember where CALLBACK is defined so for // this example I'll just define it myself..#ifndef CALLBACK#	define CALLBACK __stdcall#endif // calling conventions go in the parenthesisfoo (CALLBACK *func1)( int );foo CALLBACK myFunc( int a ){	foo my_foo;	my_foo.a = a;	return( my_foo );	}int main(int,char**){	func1 = myFunc;	foo my_foo = (*myFunc ) ( 24 );	std::cout << my_foo.a << std::endl;	return 0;}

moe.ron

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.