function-pointers

Why can't I cast a function pointer to (void *)?

こ雲淡風輕ζ 提交于 2019-11-27 09:06:07
I have a function that takes a string, an array of strings, and an array of pointers, and looks for the string in the array of strings, and returns the corresponding pointer from the array of pointers. Since I use this for several different things, the pointer array is declared as an array of (void *), and the caller should know what kind of pointers are actually there (and hence what kind of a pointer it gets back as the return value). When I pass in an array of function pointers, however, I get a warning when I compile with -Wpedantic : clang: test.c:40:8: warning: assigning to 'voidfunc'

About Pointers To Functions in function declarations

我是研究僧i 提交于 2019-11-27 09:01:44
#include<stdio.h> #include<stdlib.h> int fun1() { printf("I am fun1."); return 0; } int fun2(int fun()) { fun(); return 0; } int main() { fun2(fun1); return 0; } The above program can run. As far as I am concerned, I can understand int fun2(int (*fun)()) , but I do not know how int fun2(int fun()) works. Thank you. Nawaz When you write int fun2(int fun()) , the parameter int fun() converts into int (*fun)() , it becomes exactly equivalent to this: int fun2(int (*fun)()); A more famiiar conversion happens in case of array when you declare it as function parameter. For example, if you've this:

How Can I Pass a Member Function to a Function Pointer?

我怕爱的太早我们不能终老 提交于 2019-11-27 08:57:00
class Child; class Parent { public: void (*funcPointer)(); void (*funcPointer2)(Parent* _this); void (Child::*funcPointer3)(); }; class Child: public Parent { public: void TestFunc(){ } void Do(){ Parent p; p.funcPointer=TestFunc; // error, '=': cannot convert from 'void (__thiscall Child::* )(void)' to 'void (__cdecl *)(void)' p.funcPointer2=TestFunc; // error too, '=': cannot convert from 'void (__thiscall Child::* )(void)' to 'void (__cdecl *)(Parent *)' p.funcPointer3=TestFunc; //this works p.funcPointer3=&Child::TestFunc; // this works too. p.funcPointer3(); // error, term does not

How to print member function address in C++

社会主义新天地 提交于 2019-11-27 08:06:17
It looks like std::cout can't print member function's address, for example: #include <iostream> using std::cout; using std::endl; class TestClass { void MyFunc(void); public: void PrintMyFuncAddress(void); }; void TestClass::MyFunc(void) { return; } void TestClass::PrintMyFuncAddress(void) { printf("%p\n", &TestClass::MyFunc); cout << &TestClass::MyFunc << endl; } int main(void) { TestClass a; a.PrintMyFuncAddress(); return EXIT_SUCCESS; } the result is something like this: 003111DB 1 How can I print MyFunc 's address using std::cout ? I don't believe that there are any facilities provided by

Pointer to a C++ class member function as a global function's parameter?

自作多情 提交于 2019-11-27 08:05:25
问题 I have got a problem with calling a global function, which takes a pointer to a function as a parameter. Here is the declaration of the global function: int lmdif ( minpack_func_mn fcn, void *p, int m, int n, double *x, double *fvec, double ftol) The "minpack_func_mn" symbol is a typedef for a pointer to a function, defined as: typedef int (*minpack_func_mn)(void *p, int m, int n, const double *x, double *fvec, int iflag ); I want to call the "lmdif" function with a pointer to a function

C++ How to make function pointer to class method

我们两清 提交于 2019-11-27 08:05:18
问题 I'm having trouble making a function pointer to a class method. I made a function pointer to a non-class method and it works fine. int foo(){ return 5; } int main() { int (*pointer)() = foo; std::cout << pointer(); return 0; } I tried to apply this to have an instance variable in a class be a function pointer. This is the header file. It declares the private method Print which the variable method will point to. class Game { public: Game(); private: void Print(); void (method)( void ); }; The

Invalid use of non-static member function c++

青春壹個敷衍的年華 提交于 2019-11-27 08:00:50
问题 I am following this example. But when I compile, it returns an error: Invalid use of non-static member function at the line void(Machine:: *ptrs[])() = { Machine::off, Machine::on }; I tried to add static to void on(); at class class Machine { class State *current; public: Machine(); void setCurrent(State *s) { current = s; } static void on(); // I add static here ... static void off(); // and here }; But it complains that Invalid use of member Machine::current in static member function Can

How to pass a function pointer that points to constructor?

廉价感情. 提交于 2019-11-27 07:58:24
I'm working on implementing a reflection mechanism in C++. All objects within my code are a subclass of Object(my own generic type) that contain a static member datum of type Class. class Class{ public: Class(const std::string &n, Object *(*c)()); protected: std::string name; // Name for subclass Object *(*create)(); // Pointer to creation function for subclass }; For any subclass of Object with a static Class member datum, I want to be able to initialize 'create' with a pointer to the constructor of that subclass. You cannot take the address of a constructor (C++98 Standard 12.1/12

How do I compare two functions for pointer equality in the latest Go weekly?

别等时光非礼了梦想. 提交于 2019-11-27 07:36:23
In Go, is there any way to compare two non-nil function pointers to test for equality? My standard of equality is pointer equality. If not, is there any particular reason why pointer equality is not allowed? As of now, if I attempt to do this in the straight-forward way: package main import "fmt" func SomeFun() { } func main() { fmt.Println(SomeFun == SomeFun) } I get ./func-pointers.go:12: invalid operation: SomeFun == SomeFun (func can only be compared to nil) It is my understanding that this behavior was introduced recently. I've found an answer using the reflect package; however Atom

can void* be used to store function pointers? [duplicate]

落爺英雄遲暮 提交于 2019-11-27 06:39:20
问题 This question already has an answer here: Why are function pointers and data pointers incompatible in C/C++? 14 answers void* is defined in such a way that it could point any thing. So can it be used to point a function (int send())? int send(); void* p = send; Is it possible? When i use like this it is not showing me errors why? If not, Is there any way to store all pointers in a single variable? 回答1: No it may not. According to the C Standard (6.3.2.3 Pointers) 1 A pointer to void may be