function-pointers

Calling pointer-to-member function C++

只谈情不闲聊 提交于 2019-11-28 00:58:54
问题 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? 回答1: to call it you would do: (a->

Calling base class definition of virtual member function with function pointer

≡放荡痞女 提交于 2019-11-28 00:41:46
I want to call the base class implementation of a virtual function using a member function pointer. class Base { public: virtual void func() { cout << "base" << endl; } }; class Derived: public Base { public: void func() { cout << "derived" << endl; } void callFunc() { void (Base::*fp)() = &Base::func; (this->*fp)(); // Derived::func will be called. // In my application I store the pointer for later use, // so I can't simply do Base::func(). } }; In the code above the derived class implementation of func will be called from callFunc. Is there a way I can save a member function pointer that

Function pointer pointing to a function that takes a function pointer

雨燕双飞 提交于 2019-11-28 00:41:33
问题 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? 回答1: 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:

std::function to member function

余生长醉 提交于 2019-11-28 00:38:00
#include <functional> struct A { int func(int x, int y) { return x+y; } }; int main() { typedef std::function<int(int, int) > Funcp; A a; //Funcp func = std:::bind(&A::func, &a); Funcp func = std::bind(&A::func, a, std::placeholders::_1); return 0; } I am getting errors in both of the above bind functions: error C2825: '_Fty': must be a class or namespace when followed by '::' Where is the syntax error? I am using visual studio 2010 Funcp func = std::bind(&A::func, &a, std::placeholders::_1, std::placeholders::_2); 来源: https://stackoverflow.com/questions/5154116/stdfunction-to-member-function

C++ std::mem_fn with overloaded member function

感情迁移 提交于 2019-11-28 00:26:47
问题 When compiling the following code, Visual Studio reports: \main.cpp(21): error C2664: 'std::_Call_wrapper<std::_Callable_pmd<int ClassA::* const ,_Arg0,false>,false> std::mem_fn<void,ClassA>(int ClassA::* const )' : cannot convert argument 1 from 'overloaded-function' to 'int ClassA::* const ' 1> with 1> [ 1> _Arg0=ClassA 1> ] 1> Context does not allow for disambiguation of overloaded function Why is the compiler confused when creating mem_fptr1 ? But some how mem_fptr2 is ok when I specify

Emulating delegates with free generic type parameters in C#

不打扰是莪最后的温柔 提交于 2019-11-28 00:21:32
问题 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

Function pointer to class member function problems

冷暖自知 提交于 2019-11-28 00:18:07
First of all I have to admit that my programming skills are pretty limited and I took over a (really small) existing C++ OOP project where I try to push my own stuff in. Unfortunately I'm experiencing a problem which goes beyond my knowledge and I hope to find some help here. I'm working with a third party library (which cannot be changed) for grabbing images from a camera and will use some placeholder names here. The third party library has a function "ThirdPartyGrab" to start a continuous live grab and takes a pointer to a function which will be called every time a new frame arrives. So in a

Run-Time Check Failure #0 loading QueryFullProcessImageName from kernel32.dll

旧城冷巷雨未停 提交于 2019-11-28 00:06:15
I have an application that needs to run both on WinXP and Vista64. My program requires QueryFullProcessImageName() to work on Vista but not on XP. I try to load QueryFullProcessImageName() (instead of linking statically) via the kernel32.dll so that the same executable can run on both WinXP and Vista. The code that loads it is: //only gets called on vista bool LoadQueryFullProcessImageName() { HMODULE hDLL = LoadLibrary("kernel32.dll"); if (!hDLL) return(0); //Now use pointer to get access to functions defined in DLL fpQueryFullProcessImageName = (LPQueryFullProcessImageName)GetProcAddress

Should Taking the Address of a Templatized Function Trigger its Compilation?

空扰寡人 提交于 2019-11-27 23:10:24
I got an official answer to this question that decltype should not trigger function compilation. In fact decltype on a function that is declared but not defined is legal. Next question, should taking the address of a function trigger the compilation of a function? Take this example : template <typename T> void foo(T&& x) { x.func(); } int main() { auto bar = &foo<int>; } All the compilers I've tested fail with an error like: Request for member func in x , which is of non-class type int But if I just define foo and don't declare it, the code compiles fine. Can someone provide me with an

Pointers to members representations

允我心安 提交于 2019-11-27 23:01:08
I'm trying to make some callbacks from member functions and everything was ok until I tried to use a template class derived from 2 classes as callback object when I got the following error: error C2440: 'reinterpret_cast' : Pointers to members have different representations; cannot cast between them This thing signaled me that member function pointers have different representations(doh!) What are these representations? What is the difference between them? Danny Kalev explains this quite nicely : The Underlying Representation of Pointers to Members Although pointers to members behave like