function-pointers

C++ function pointers and classes

ok say I have void Render(void(*Call)()) { D3dDevice->BeginScene(); Call(); D3dDevice->EndScene(); D3dDevice->Present(0,0,0,0); } This is fine as long as the function I want to use to render is a function or static class method Render(MainMenuRender); Render(MainMenu::Render); However I really want to be able to use...

What is the cost of using a pointer to member function vs. a switch?

I have the following situation: class A { public: A(int whichFoo); int foo1(); int foo2(); int foo3(); int callFoo(); // cals one of the foo's depending on the value of whichFoo }; In my current implementation I save the value of whichFoo in a data member in the constructor and use a switch in callFoo() to decide ...

What's the nearest substitute for a function pointer in Java?

I have a method that's about 10 lines of code. I want to create more methods that do the exact same thing except for a calculation that's going to change one line of code. This is a perfect application for passing in a function pointer to replace that one line, but Java doesn't have function pointers. What's my best alternative? ...

Functions in C

Can we call functions using function pointer? if yes how? ...

Does TCL have some concept of function pointers?

Working with TCL and I'd like to implement something like the Strategy Pattern. I want to pass in the "strategy" for printing output in a TCL function, so I can easily switch between printing to the screen and printing to a log file. What's the best way to do this in TCL? ...

Function pointers in C - address operator "unnecessary"

Hello, Using qsort in C we pass in a comparison function e.g. int cmp(const void*, const void*); the protoype of qsort expects a int (* )(const void* , const void*) so we call: qsort(..., cmp); but it is equally valid to call: qsort(..., &cmp); and this is what we would have to do if we passed in a static member-function in C++...

How do I write a dispatcher, if my compiler's support for pointers-to-functions is broken?

I am working on an embedded application where the device is controlled through a command interface. I mocked the command dispatcher in VC and had it working to my satisfaction; but when I then moved the code over to the embedded environment, I found out that the compiler has a broken implementation of pointer-to-func's. Here's how I or...

Using far function pointers

I realize that far is compiler specific, but my expectation is that the placement of the far specifier should make sense to those who really understand pointers. So, I have two applications that share the processor's entire memory space. App A needs to call function foo that exists in app B. I know the memory location of function foo....

anonymous unbound functions in python

I would like to do something like the following: def add(a, b): #some code def subtract(a, b): #some code operations = [add, subtract] operations[0]( 5,3) operations[1](5,3) In python, is it possible to assign something like a function pointer? ...

How does one declare an array of constant function pointers in C?

I need to declare an array of pointers to functions like so: extern void function1(void); extern void function2(void); ... void (*MESSAGE_HANDLERS[])(void) = { function1, function2, ... }; However, I want the the array to be declared as constant -- both the data in the array and the pointer to the data. Unfortunately, I do n...

How to get function's name from function's pointer in C?

How to get function's name from function's pointer in C? Edit: The real case is: I'm writing a linux kernel module and I'm calling kernel functions. Some of these functions are pointers and I want to inspect the code of that function in the kernel source. But I don't know which function it is pointing to. I thought it could be done beca...

Function pointers/delegates in Java?

Hello, for my Java game server I send the Action ID of the packet which basically tells the server what the packet is for. I want to map each Action ID (an integer) to a function. Is there a way of doing this without using a switch? ...

calling code stored in the heap from vc++

Imagine I am doing something like this: void *p = malloc (1000); *((char*)p) = some_opcode; *((char*)p+1) = another_opcode; // for the sake of the example: the opcodes are ok .... etc... How can I define a function pointer to call p as if it was a function? (i'm using VC++ 2008 express). Thanks ...

How can I pass a class member function as a callback?

Hi, I'm using an API that requires me to pass a function pointer as a callback. I'm trying to use this API from my class but I'm getting compilation errors. Here is what I did from my constructor: m_cRedundencyManager->Init(this->RedundencyManagerCallBack); This doesn't compile - I get the following error: Error 8 error C3867...

C++, equivalence between pointer-to-functions and pointer-to-member-functions?

I'm used to thinking of member functions as just being a special case of normal functions, where member functions have an extra parameter at the beginning of their parameter list for the 'this' pointer, that is, the object on which the member function is supposed to act. I've used boost::function this way in the past and never encountere...

How to pass a COM method as a function argument? And Microsoft Compiler error C3867.

Hello, I would like to pass a COM method as a function argument but I get this error (Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 15.00.30729.01 for 80x86): error C3867: 'IDispatch::GetTypeInfoCount': function call missing argument list; use '&IDispatch::GetTypeInfoCount' to create a pointer to member What am I missing? Tha...

Function pointer to class member function problems

First of all I have to admit that my programming skills are pretty limited and I took over a (really small) existing C++ OOP project where I try to push my own stuff in. Unfortunately I'm experiencing a problem which goes beyond my knowledge and I hope to find some help here. I'm working with a third party library (which cannot be change...

Callback functions in Java

Is there a way to do pass a call back function in a Java method? The bahaviour I'm trying to mimic is a .Net Delegate being passed to a function. I've seem people suggesting creating a separate object but that seems overkill, however I am aware that sometimes overkill is the only way to do things. ...

Can I simplify this?

typedef void (FunctionSet::* Function)(); class MyFunctionSet : public FunctionSet { protected: void addFunctions() { addFunction(Function(&MyFunctionSet::function1)); } void function1() { // Do something. } }; The addFunction method adds the function to a list in the base class, which can then be en...

Pure Virtual Method VS. Function Pointer

Hey all, Recently I've been designing a Thread class library, I've made a Thread abstract class like the following: class Thread { public: run() { /*start the thread*/ } kill() { /*stop the thread*/ } protected: virtual int doOperation(unsigned int, void *) = 0; }; Real thread classes would inherit this abstract class and ...