function-pointers

How to create a typedef for function pointers

这一生的挚爱 提交于 2019-11-28 04:41:05
I think it would be easier to use function pointers if I created a typedef for a function pointer, but I seem to be getting myself tripped up on some syntax or usage or something about typedef for function pointers, and I could use some help. I've got int foo(int i){ return i + 1;} typedef <???> g; int hvar; hvar = g(3) That's basically what I'm trying to accomplish I'm a rather new C programmer and this is throwing me too much. What replaces <???> ? Your question isn't clear, but I think you might want something like this: int foo(int i){ return i + 1;} typedef int (*g)(int); // Declare

Pointers to static methods in Python

痞子三分冷 提交于 2019-11-28 04:25:46
问题 Why is it that in the following code, using a class variable as a method pointer results in unbound method error, while using an ordinary variable works fine: class Cmd: cmd = None @staticmethod def cmdOne(): print 'cmd one' @staticmethod def cmdTwo(): print 'cmd two' def main(): cmd = Cmd.cmdOne cmd() # works fine Cmd.cmd = Cmd.cmdOne Cmd.cmd() # unbound error !! if __name__=="__main__": main() The full error: TypeError: unbound method cmdOne() must be called with Cmd instance as first

C++ class member function pointer to function pointer

≡放荡痞女 提交于 2019-11-28 04:21:51
问题 I am using luabind as my lua to C++ wrapper. Luabind offers a method to use my own callback function to handle exceptions thrown by lua, set_pcall_callback(). So I paraphrased an example from the documentation, the changes being the logger->log() function and putting the function in a class called 'Engine', so instead of it being a regular global function it is now a member function, which is where my problem seems to be. Here are the relevant code snips: class Engine //Whole class not shown

generic member function pointer as a template parameter

老子叫甜甜 提交于 2019-11-28 03:51:31
Consider this code: #include <iostream> using namespace std; class hello{ public: void f(){ cout<<"f"<<endl; } virtual void ff(){ cout<<"ff"<<endl; } }; #define call_mem_fn(object, ptr) ((object).*(ptr)) template<R (C::*ptr_to_mem)(Args...)> void proxycall(C& obj){ cout<<"hello"<<endl; call_mem_fn(obj, ptr_to_mem)(); } int main(){ hello obj; proxycall<&hello::f>(obj); } Of course this won't compile at line 16, because the compiler doesn't know what R , C and Args , are. But there's another problem: if one tries to define those template parameters right before ptr_to_mem , he runs into this bad

Executing machine code in memory

人走茶凉 提交于 2019-11-28 03:41:45
I'm trying to figure out how to execute machine code stored in memory. I have the following code: #include <stdio.h> #include <stdlib.h> int main(int argc, char* argv[]) { FILE* f = fopen(argv[1], "rb"); fseek(f, 0, SEEK_END); unsigned int len = ftell(f); fseek(f, 0, SEEK_SET); char* bin = (char*)malloc(len); fread(bin, 1, len, f); fclose(f); return ((int (*)(int, char *)) bin)(argc-1, argv[1]); } The code above compiles fine in GCC, but when I try and execute the program from the command line like this: ./my_prog /bin/echo hello The program segfaults. I've figured out the problem is on the

Virtual Methods or Function Pointers

孤街浪徒 提交于 2019-11-28 03:09:11
When implementing polymorphic behavior in C++ one can either use a pure virtual method or one can use function pointers (or functors). For example an asynchronous callback can be implemented by: Approach 1 class Callback { public: Callback(); ~Callback(); void go(); protected: virtual void doGo() = 0; }; //Constructor and Destructor void Callback::go() { doGo(); } So to use the callback here, you would need to override the doGo() method to call whatever function you want Approach 2 typedef void (CallbackFunction*)(void*) class Callback { public: Callback(CallbackFunction* func, void* param);

How does the template parameter of std::function work? (implementation)

断了今生、忘了曾经 提交于 2019-11-28 02:50:57
In Bjarne Stroustrup 's home page ( C++11 FAQ ): struct X { int foo(int); }; std::function<int(X*, int)> f; f = &X::foo; //pointer to member X x; int v = f(&x, 5); //call X::foo() for x with 5 How does it work? How does std::function call a foo member function ? The template parameter is int(X*, int) , is &X::foo converted from the member function pointer to a non-member function pointer ?! (int(*)(X*, int))&X::foo //casting (int(X::*)(int) to (int(*)(X*, int)) To clarify: I know that we don't need to cast any pointer to use std::function , but I don't know how the internals of std::function

C late binding with unknown arguments

可紊 提交于 2019-11-28 02:17:56
I am presently in a case where I need to call a lot of function pointers that has been extracted at runtime. The problem is that the arguments are unknown at compilation time. But, at runtime I receive datas that allows me to know the arguments of the function and I can even store the arguments in a char* array. The problem is that I don't have a function pointer model to cast it into. In high level language, I know there is function like "InvokeMethode(String name,Byte[] args)" that interpret the bytes array like arguments. Since reflection does not exist in C, I have no hope to see this with

are there function pointers in c#?

与世无争的帅哥 提交于 2019-11-28 01:54:05
I am trying to learn some c# coding and wondering if the c++ concept of function pointers is included in c#. I see there are such things as delegates. Are they the same concept? or do they differ on a more fundamental level? Delegates are essentially function pointers, but with extra multicast capabilities built in. So you can assign several functions to the same delegate, and they will all be called in sequence when the delegate is called. Delegates also have built-in asynchronous interfaces, and have co/contra variance when assigning new functions to a delegate (and, in .NET 4, when passing

Typedeffing a function (NOT a function pointer)

跟風遠走 提交于 2019-11-28 01:22:53
问题 typedef void int_void(int); int_void is a function taking an integer and returning nothing. My question is: can it be used "alone", without a pointer? That is, is it possible to use it as simply int_void and not int_void* ? typedef void int_void(int); int_void test; This code compiles. But can test be somehow used or assigned to something (without a cast)? /* Even this does not work (error: assignment of function) */ typedef void int_void(int); int_void test, test2; test = test2; 回答1: What