member-function-pointers

How to define a general member function pointer

自闭症网瘾萝莉.ら 提交于 2019-12-21 04:08:47
问题 I have created a Timer class that must call a callback method when the timer has expired. Currently I have it working with normal function pointers (they are declared as void (*)(void), when the Elapsed event happens the function pointer is called. Is possible to do the same thing with a member function that has also the signature void (AnyClass::*)(void)? Thanks mates. EDIT: This code has to work on Windows and also on a real-time OS (VxWorks) so not using external libraries would be great.

C++ member function as callback function to external library

瘦欲@ 提交于 2019-12-20 07:41:02
问题 So below is a basic idea of what I'm trying to do. I have an external library that I would like to use in an existing project. I cannot change anything in the external library or the main function in the existing project of course. The problem I face is how to pass a callback function I make in my class to this external function as a pointer to function. At the same time, this callback function has to have access to members of the class so I cannot simply make it static. How can I do it?

C++ map of “events” and member function pointers

不羁的心 提交于 2019-12-20 06:21:28
问题 I've managed to write a template class to work like a callback, learned from the accepted answer of this question How to define a general member function pointer. I wish to have a map of string keys and callback values so that I can invoke the proper callback that matches a string. This would be fine but I need the map to support callbacks from different classes. Right now it can only work with one class. It can be any class because of the template but only a collection of callbacks from the

Can I legally cast a member function pointer to a function pointer?

拜拜、爱过 提交于 2019-12-19 09:23:14
问题 I've inherited some C++ code and I've been tasked with getting rid of warnings. Here we have a member function pointer being cast to a function pointer. I understand that member function pointers are "different" from function pointers, in that there is an implicit 'this' parameter involved under the hood. However my predecessor appears to have made explicit use of this fact, by casting from a member function pointer to a function pointer with an additional first parameter inserted. My

Protected member function address in derived class is not accessible

佐手、 提交于 2019-12-19 05:52:27
问题 #include <iostream> class A { protected: void foo() {} }; class B : public A { public: void bar() { std::cout << (&A::foo) << std::endl; } }; int main() { B b; b.bar(); } Here I am trying to get address of protected member function of base class. I am getting this error. main.cpp: In member function ‘void B::bar()’: main.cpp:5: error: ‘void A::foo()’ is protected main.cpp:13: error: within this context make: *** [all] Error 1 Changing foo to public works. Also printing &B::foo works. Can you

Protected member function address in derived class is not accessible

♀尐吖头ヾ 提交于 2019-12-19 05:51:03
问题 #include <iostream> class A { protected: void foo() {} }; class B : public A { public: void bar() { std::cout << (&A::foo) << std::endl; } }; int main() { B b; b.bar(); } Here I am trying to get address of protected member function of base class. I am getting this error. main.cpp: In member function ‘void B::bar()’: main.cpp:5: error: ‘void A::foo()’ is protected main.cpp:13: error: within this context make: *** [all] Error 1 Changing foo to public works. Also printing &B::foo works. Can you

Can I get the Owning Object of a Member Function Template Parameter?

喜欢而已 提交于 2019-12-18 19:09:16
问题 Given a object: struct foo { void func(); }; Now given the templatized function declaration: template<typename T, T F> void bar(); So bar will be taking in a member function like so: bar<decltype(&foo::func), &foo::func>() In the body of bar I want to recover the type foo from T . Can I do that? I want to be able to do something like this: get_obj<T> myfoo; (myfoo.*F)(); I know that get_obj isn't a thing, but would there be a way to write it? 回答1: template<class T> struct get_memfun_class;

Why must I use address-of operator to get a pointer to a member function?

牧云@^-^@ 提交于 2019-12-18 08:35:43
问题 struct A { void f() {} }; void f() {} int main() { auto p1 = &f; // ok auto p2 = f; // ok auto p3 = &A::f; // ok // // error : call to non-static member function // without an object argument // auto p4 = A::f; // Why not ok? } Why must I use address-of operator to get a pointer to a member function? 回答1: auto p1 = &f; // ok auto p2 = f; // ok The first is more or less the right thing. But because non-member functions have implicit conversions to pointers, the & isn't necessary. C++ makes

Why must I use address-of operator to get a pointer to a member function?

雨燕双飞 提交于 2019-12-18 08:35:27
问题 struct A { void f() {} }; void f() {} int main() { auto p1 = &f; // ok auto p2 = f; // ok auto p3 = &A::f; // ok // // error : call to non-static member function // without an object argument // auto p4 = A::f; // Why not ok? } Why must I use address-of operator to get a pointer to a member function? 回答1: auto p1 = &f; // ok auto p2 = f; // ok The first is more or less the right thing. But because non-member functions have implicit conversions to pointers, the & isn't necessary. C++ makes

Object-Oriented Callbacks for C++?

天涯浪子 提交于 2019-12-18 03:36:12
问题 Is there some library that allows me to easily and conveniently create Object-Oriented callbacks in c++? the language Eiffel for example has the concept of "agents" which more or less work like this: class Foo{ public: Bar* bar; Foo(){ bar = new Bar(); bar->publisher.extend(agent say(?,"Hi from Foo!", ?)); bar->invokeCallback(); } say(string strA, string strB, int number){ print(strA + " " + strB + " " + number.out); } } class Bar{ public: ActionSequence<string, int> publisher; Bar(){}