function-pointers

Calling a member function pointer stored in a std map

夙愿已清 提交于 2019-12-31 03:08:16
问题 I'm storing a map in a class that has strings as keys and pointers to member functions as values. I'm having trouble calling the right function throw the function pointer. Here is the code: #include <iostream> #include <string> #include <map> using namespace std; class Preprocessor; typedef void (Preprocessor::*function)(); class Preprocessor { public: Preprocessor(); ~Preprocessor(); void processing(const string before_processing); private: void take_new_key(); map<string, function> srch

Reference to member function? [duplicate]

情到浓时终转凉″ 提交于 2019-12-30 16:27:08
问题 This question already has an answer here : Why doesn't reference-to-member exist in C++? (1 answer) Closed 5 years ago . I recently find out that there is a reference-to-function concept in C++ :). So as there are pointer-to-function and pointer-to-member-function different types. The question arises. Is there a "reference-to-member-function" concept? I tried to compile the following code, but GCC 3.4.6 gives an error. #include <iostream> using namespace std; class A { public: virtual void Af

is it safe to pass function pointers around and compare them just like normal object's pointers?

跟風遠走 提交于 2019-12-30 09:59:14
问题 Here is the thing, I have several functions, void foo() {} void bar() {} And I want to pass these functions around just like ordinary objects' pointers, int main() { void (*fptr1)() = foo; void (*fptr2)() = fptr1; void (*fptr3)() = bar; if (fptr1 == foo) printf("foo function\n"); if (fptr2 == foo) printf("foo function\n"); if (fptr3 == foo) printf("foo function\n") } Can I use these function pointers this way? And I wrote a program to test it, seems like ok. Furthermore, I think, not like

Is it possible to swap C functions?

若如初见. 提交于 2019-12-30 06:59:09
问题 Looking to see if anyone knows if its possible to swap C functions...? void swap2(int(*a)(int), int(*b)(int)) { int(*temp)(int) = a; *a = *b; *b = temp; // Gives 'Non-object type 'int (int)' is not assignable } swap2(&funcA, &funcB); EDIT More data here as to intention -- Some answers have been provided below which do work such as creating the function ptr using typedef , pointing them to the functions and switching those, which lets you invoke the new swapped ptrs successfully. BUT calling

Procedure Pointer, Derived Type

不问归期 提交于 2019-12-30 06:38:11
问题 The following doesnt compile in Intel Fortran XE 2011: TYPE type1 procedure(interface1),POINTER::p END TYPE type1 ABSTRACT INTERFACE integer function interface1(a) real,intent(in)::a END function interface1 END INTERFACE The error: error #8262: The passed-object dummy argument must be dummy data object with the same declared type as the type being defined. 回答1: Add the nopass attribute to the declaration of the procedure pointer component. procedure(interface1), pointer, nopass :: p Edit: In

How to properly define a function pointer in struct, which takes struct as a pointer?

丶灬走出姿态 提交于 2019-12-30 06:28:07
问题 I have a struct with a callback function, the callback function needs a pointer to the structure in order to do its operation. How do I properly define these elements such that is will compile without warnings? typedef struct { // some fields required for processing... int (*doAction)(struct pr_PendingResponseItem *pr); } pr_PendingResponseItem; If I remove the "struct" attribute on the pr parameter, I get an error. If I leave it in, I get a warning: "its scope is only this definition or

Function pointer to different functions with different arguments in C

百般思念 提交于 2019-12-30 04:12:05
问题 I have two functions with variable number and types of arguments double my_func_one(double x, double a, double b, double c) { return x + a + b + c } double my_func_two(double x, double p[], double c) { return x + p[0] + p[1] + c } I want to use a pointer to a function to the functions I defined above based on a some condition getting true e.g. if (true == condition_1) pfunc = my_func_one; else if (true == condition_2) pfunc = my_func_two; // The function that will use the function I passed to

How to pass anonymous functions as parameters in Rust?

早过忘川 提交于 2019-12-30 04:00:49
问题 I've been playing around with Rust the past week. I can't seem to figure out how to pass a function that is defined as a parameter when calling the method, and haven't come across any documentation that shows them being used in that fashion. Is it possible to define a function in the parameter list when calling a function in Rust ? This is what I've tried so far... fn main() { // This works thing_to_do(able_to_pass); // Does not work thing_to_do(fn() { println!("found fn in indent position");

C: How can I use a single function pointer array for functions with variable parameter counts?

一曲冷凌霜 提交于 2019-12-30 00:35:16
问题 The question pretty much says it all. I'm not sure how to do this and haven't come anywhere near anything that works. Here's some example functions: add(int x, int y) { return x+y; } and, mean(int x1, int y1, int x2, int y2) { return (x1 + y1 + x2 + y2) / 4; } So far I've tried using typedef with both, but I can't figure how to make something point to one of either type: typedef int (*mathfunc2)(int x, int y); typedef int (*mathfunc4)(int x1, int y1, int x2, int y2); ????? func_table[2] =

Pointer to current function

时光怂恿深爱的人放手 提交于 2019-12-29 08:51:46
问题 Is there any way to get a pointer to the current function, maybe through gcc extensions or some other trickery? Edit I'm curious whether it is possible to get the function pointer without ever explicitly using the function's name. I thought I had a good reason for wanting this, realized that I didn't really, but am still curious if it is possible. 回答1: This isn't especially portable, but should work on at least some platforms (i.e., Linux and OSX, where I can check the documentation; it