function-pointers

Store Function Pointers to any Member Function

蓝咒 提交于 2019-11-29 10:24:13
问题 My Event Manager For a event manager I need to store many pointers to functions in a vector to call them when the event is triggered. (I will provide the source code of the EventFunction helper class at the end of this question.) // an event is defined by a string name and a number typedef pair<string, int> EventKey; // EventFunction holds a pointer to a listener function with or without data parameter typedef unordered_map<EventKey, vector<EventFunction>> ListEvent; // stores all events and

Typedeffing a function (NOT a function pointer)

我的未来我决定 提交于 2019-11-29 07:55:34
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; What happens is that you get a shorter declaration for functions. You can call test , but you will need an actual

How to use varargs in conjunction with function pointers in C on Win64?

喜夏-厌秋 提交于 2019-11-29 07:21:50
Consider the following C program: #include <stdio.h> #include <stdarg.h> typedef void (callptr)(); static void fixed(void *something, double val) { printf("%f\n", val); } static void dynamic(void *something, ...) { va_list args; va_start(args, something); double arg = va_arg(args, double); printf("%f\n", arg); } int main() { double x = 1337.1337; callptr *dynamic_func = (callptr *) &dynamic; dynamic_func(NULL, x); callptr *fixed_func = (callptr *) &fixed; fixed_func(NULL, x); printf("%f\n", x); } Basically, the idea is to store a function with variable arguments in a "generic" function pointer

Default value of a function pointer in C++

a 夏天 提交于 2019-11-29 07:19:50
What is the default value of a function pointer in C++? (Apparently it can't be NULL , so what is it?) How is this program supposed to behave and why? struct S { void (*f)(); }; int main() { S s = S(); s.f(); // What is the value of s.f? } In your case the object s is zero-initialized which means the function pointer is NULL . struct S { void (*f)(); }; int main() { S s = S(); if ( s.f == NULL) std::cout << "s.f is NULL" << std::endl; } Output: s.f is NULL Online demo . WhozCraig First any pointer can be null. It is the one universal truth about pointers. That said, yours will be null, but not

Calling pointer-to-member function C++

邮差的信 提交于 2019-11-29 07:11:46
I have a pointer to a member function defined within a class, e.g.: class Example { void (Example::*foo)(); void foo2(); }; In my main code, I then set foo as: Example *a; a->foo = &Example::foo2; However, when I try to call foo: a->foo(); I get the following compile time error: "error: expression preceding parentheses of apparent call must have (pointer-to-) function type". I'm assuming I'm getting the syntax wrong somewhere, can someone point it out to me? to call it you would do: (a->*(a->foo))() (a->*X)(...) - dereferences a member function pointer - the parens around a->*X are important

Function pointer pointing to a function that takes a function pointer

给你一囗甜甜゛ 提交于 2019-11-29 07:02:48
How do I declare a function pointer that points to a function taking the same function pointer as the argument? I've tried the following without success: typedef void (*fnptr)(void (*)()); void func(fnptr) { /* ... */ } void func2(fnptr) { /* ... */ } void main() { fnptr fn = &func; func2(fn); } Is this possible? I very much doubt it, but you can get the needed recursion by introducing a struct. struct Rec; typedef void (*RecFun)(const Rec&); struct Rec { RecFun fun; }; Example of use: #include <iostream> void nothing(const Rec& rec) {} Rec recNothing = { nothing }; void f(const Rec& rec) {

Emulating delegates with free generic type parameters in C#

廉价感情. 提交于 2019-11-29 06:53:16
This is a hard question about language design, patterns and semantics. Please, don't down-vote just because you don't see the practical value. First, let's think about functions and their parameters. Then we'll look at the analogies between functions with their parameters/arguments and generic classes/functions with their type-parameters/type-arguments. Functions are blocks of code with some unspecified values called " parameters ". You supply the arguments and receive the result. Generic classes are classes with some unspecified " type-parameters ". You supply the type-arguments and then you

C++ instantiate template class from DLL

一世执手 提交于 2019-11-29 05:18:50
I tried to make a DLL that contains: base template class , only with a virtual destructor and no attributes ( I called it MatrixInterface ) a derived class with constructors, destructor, operator= and attributes ( matrix class ) a function that returns a base class pointer to a new derived object: #ifdef BUILD_DLL #define DLL_EXPORT __declspec(dllexport) #else #define DLL_EXPORT __declspec(dllimport) #endif template<class T> MatrixInterface<T> DLL_EXPORT * CreateMatrixInstance(unsigned int n,unsigned int m) { return new matrix<T>(n,m); } I wanted to instatiate the matrix class in my program

Functional Programming (Currying) in C / Issue with Types

眉间皱痕 提交于 2019-11-29 03:44:52
As a dyed-in-the-wool functional programmer I find it hard not to try to shoehorn my favourite paradigm into whatever language I'm using. While writing some C I found I'd like to curry one of my functions, and then pass around the partially applied function. After reading Is there a way to do currying in C? and heeding the warnings at http://gcc.gnu.org/onlinedocs/gcc/Nested-Functions.html#Nested-Functions I came up with: #include <stdio.h> typedef int (*function) (int); function g (int a) { int f (int b) { return a+b; } return f; } int f1(function f){ return f(1);} int main () { printf ("(g(2

Is it safe to cast a lambda function to a function pointer?

∥☆過路亽.° 提交于 2019-11-29 03:42:09
I have this code: void foo(void (*bar)()) { bar(); } int main() { foo([] { int x = 2; }); } However, I'm worried that this will suffer the same fate as: struct X { int i; }; void foo(X* x) { x->i = 2; } int main() { foo(&X()); } Which takes the address of a local variable. Is the first example completely safe? Yes I believe the first example is safe, regardless of the life-time of all the temporaries created during the evaluation of the full-expression that involves the capture-less lambda-expression. Per the working draft (n3485) 5.1.2 [expr.prim.lambda] p6 The closure type for a lambda