function-pointers

Why is Taking the Address of a Function That is Declared Only Working?

半世苍凉 提交于 2019-12-02 09:15:39
I've asked a question here about whether taking the address of a function forces the compilation of said function specifically with regard to Substitution-Failure-Is-Not-An-Error. The most direct answer to this can be found here : Informally, an object is odr-used if its address is taken, or a reference is bound to it, and a function is odr-used if a function call to it is made or its address is taken. If an object or a function is odr-used, its definition must exist somewhere in the program; a violation of that is a link-time error. But all the compilers I've tested show this as perfectly

Calling a member function using function pointers from another class's instance

自古美人都是妖i 提交于 2019-12-02 08:42:43
#include<iostream> #include<conio.h> using namespace std; class Base; typedef void (Base::*function)(); class Base { public: function f; Base() { cout<<"Base Class constructor"<<endl; } virtual void g()=0; virtual void h()=0; }; class Der:public Base { public: Der():Base() { cout<<"Derived Class Constructor"<<endl; f=(function)(&Der::g); } void g() { cout<<endl; cout<<"Function g in Derived class"<<endl; } void h() { cout<<"Function h in Derived class"<<endl; } }; class Handler { Base *b; public: Handler(Base *base):b(base) { } void CallFunction() { cout<<"CallFunction in Handler"<<endl; (b->

Why am I getting a Linker error with template function pointer?

筅森魡賤 提交于 2019-12-02 07:10:47
I have a class EventMgr which has a template function to register a listener. But, when I register a listener, linker gives me a " error LNK2019: unresolved external symbol ". Appetizer code: class EventMgr { template< class T, class EvenT> void RegisterListener(T* listener, int EventType, void (T::*MemFunc)(EvenT*) ); } SoundMgr(which is a listener) tries to register for event: SoundMgr::SoundMgr(void) { EventManager::GetInstance()->RegisterListener(this, 1, (&SoundMgr::handleBulletFired)); } I'm not sure why it won't link. Why can it not find the reference types ? If you simply declared the

How to create a container that holds different types of function pointers in C++?

你离开我真会死。 提交于 2019-12-02 07:07:48
问题 I'm doing a linear genetic programming project, where programs are bred and evolved by means of natural evolution mechanisms. Their "DNA" is basically a container (I've used arrays and vectors successfully) which contain function pointers to a set of functions available. Now, for simple problems, such as mathematical problems, I could use one type-defined function pointer which could point to functions that all return a double and all take as parameters two doubles. Unfortunately this is not

Why do function objects evaluate to True in python?

。_饼干妹妹 提交于 2019-12-02 06:54:26
问题 In python it is valid to make a construction like: def a(): return 0 if a: print "Function object was considered True" else: print "Function object was considered False" I wish to ask what is the logic that a function pointer is evaluated to True. Why was this kind of construction inserted in the language? 回答1: A lot of things evaluate to True in Python. From the documentation on Boolean operators: In the context of Boolean operations, and also when expressions are used by control flow

struct pointer function points to other function of other struct

瘦欲@ 提交于 2019-12-02 06:44:47
I was wondering if is possible to point a function of other structure into of a structure: Example: typedef struct { int func(int z) { return z * 2; } } sta; typedef struct { int(*this.func)(int); } stah; int main() { sta sa; stah sah; sah.func = &sa.func; return 0; } it's possible this in a struct? skypjack The declaration of func should look like this: int(sta::*func)(int); Or, alternatively: using my_type = int(sta::*)(int); my_type func; This is easier to read: my_type is an alias for the type pointer to a member function of sta that gets an int and returns an int . func is nothing more

Why am I getting a Linker error with template function pointer?

送分小仙女□ 提交于 2019-12-02 06:40:56
问题 I have a class EventMgr which has a template function to register a listener. But, when I register a listener, linker gives me a " error LNK2019: unresolved external symbol ". Appetizer code: class EventMgr { template< class T, class EvenT> void RegisterListener(T* listener, int EventType, void (T::*MemFunc)(EvenT*) ); } SoundMgr(which is a listener) tries to register for event: SoundMgr::SoundMgr(void) { EventManager::GetInstance()->RegisterListener(this, 1, (&SoundMgr::handleBulletFired));

Why do function pointers all have the same value?

天涯浪子 提交于 2019-12-02 06:23:19
问题 For example: using namespace std; #include <iostream> void funcOne() { } void funcTwo( int x ) { } int main() { void (*ptrOne)() = funcOne; cout << ptrOne << endl; //prints 1 void (*ptrTwo)( int x ) = funcTwo; cout << ptrTwo << endl; //prints 1 int (*ptrMain)() = main; cout << ptrMain << endl; //prints 1 } Does anyone know the reasoning behind this? At first I thought it was because the functions don't exist in memory since I never call on them, and thus they never get added to the stack. But

C++ Converting function pointer to unique “hash” key

旧时模样 提交于 2019-12-02 05:47:14
问题 Se original question in the bottom. I think I understand what you guys are saying now – that because the internal structure of the member function pointer is compiler/machine specific it is really not possible to do that I am trying to. So even though it works when I test it – I have no guarantee that it will on other compilers/machines. Is there another way to go about what I want then? I have a template class and a base template class for that class, and I have a delegate class which

Exporting a function pointer from dll

大兔子大兔子 提交于 2019-12-02 05:45:11
I have a function pointer in a dll file (in implementation, not header). How can I call the function pointed to by this pointer in the exe source code? you can export a function returning the pointer. Header: typedef void ( *MyPtr ) (); __declspec( dllexport ) MyPtr GetMyPtr(); Source: MyPtr GetMyPtr() { //retunr the function pointer here } 来源: https://stackoverflow.com/questions/7764074/exporting-a-function-pointer-from-dll