member-function-pointers

C++ - is it possible to extract class and argument types from a member function type in a template?

假装没事ソ 提交于 2019-12-03 14:56:00
I would like to wrap member functions that conform to the type 'void (ClassType::Function)(ArgType)' with a templated class. Later, I want to pass an instance of ClassType to an instance of this template and have it invoke the wrapped method: class Foo { public: Foo() : f_(0.0) {} void set(double v) { f_ = v * 2.1; } double get() { return f_; } private: double f_; }; template <typename ArgType, typename ClassType, void (ClassType::*Method)(ArgType)> class Wrapper { public: explicit Wrapper(ClassType *cls) : cls_(cls) {} void do_something(ArgType value) { (cls_->*Method)(value); } private:

How to define a general member function pointer

时间秒杀一切 提交于 2019-12-03 12:39:38
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. EDIT2: Just to be sure, what I need is to have a Timer class that take an argument at the Constructor

How to register a derived class member function pointer with a base class

大城市里の小女人 提交于 2019-12-03 03:13:28
As opposed to virtual member functions, I need a solution where a function implemented at each level class derivation can be registered for later call by the base class. ( Not just the most derived implementation) To do this, I was thinking on providing a mechanism for derived classes to register their function with the base class such as during the derived class constructor. I'm having trouble with the member function pointer argument though. I was thinking that Derived is derived from Base, the this pointer should be automatically casted. Can this be done close to what I am trying or do I

Member function pointer issue with standard library methods

邮差的信 提交于 2019-12-02 16:33:39
问题 This question is spawned from Passing a member function pointer to an overloaded class method into a template function. You need not read that to understand this question. Probably both the questions will have the same answer. I am getting compiler error for below simple code. #include<set> template<typename Return, typename T> T ReceiveFuncPtr (Return (T::*Method)(const int&)) { T obj; // Found and declared an object of actual container class (obj.*Method)(1); // Some processing return obj;

C++ member function as callback function to external library

我怕爱的太早我们不能终老 提交于 2019-12-02 13:19:23
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? Class ExternalClass //This I cannot mess with. { //somestuff void ExternalFunc (void(* callback)(int, const

Member function pointer issue with standard library methods

时光总嘲笑我的痴心妄想 提交于 2019-12-02 11:08:14
This question is spawned from Passing a member function pointer to an overloaded class method into a template function . You need not read that to understand this question. Probably both the questions will have the same answer. I am getting compiler error for below simple code . #include<set> template<typename Return, typename T> T ReceiveFuncPtr (Return (T::*Method)(const int&)) { T obj; // Found and declared an object of actual container class (obj.*Method)(1); // Some processing return obj; // Returned that container class object with RVO } int main () { ReceiveFuncPtr(&std::set<int>:

How to call a function using pointer-to-member-function

∥☆過路亽.° 提交于 2019-12-02 03:10:06
I have a class: class A { void test_func_0(int); void run(); typedef void(A::*test_func_t)(int); struct test_case_t{ test_func_t test_func; } test_case[100]; }; Now I want to call test_func() inside run(): void A::run() { test_case[0].test_func = &test_func_0; test_case[0].*(test_func)(1); } The last line of my code, doesn't work(compile error), no matter what combination I try. Use this: void A::run() { test_case[0].test_func = &A::test_func_0; (this->*(test_case[0].test_func))(1); } Notice that you had 2 errors. The first one was how you formed the member-function-pointer. Note that the only

How do I declare, create, and use method pointers in Swift?

泪湿孤枕 提交于 2019-12-01 21:21:20
问题 I'm not talking about pointers to C functions, but to a method within a Swift type. struct Test: GeneratorType { var methodPointer: mutating () -> Bool? // Non-working guess var which: Bool init() { which = false methodPointer = which ? &testMethod1 : &testMethod2 // Also non-working guess } //... } The compiler says " mutating " isn't legal as part of a function declaration. (Actually, it just suggests a semi-colon there.) And for the pointer initialization (after I remove mutating ), the

Getting a function name (__func__) from a class T and a pointer to member function void(T::*pmf)()

守給你的承諾、 提交于 2019-12-01 11:53:38
Is it possible to write some f() template function that takes a type T and a pointer to member function of signature void(T::*pmf)() as (template and/or function) arguments and returns a const char* that points to the member function's __func__ variable (or to the mangled function name)? EDIT : I am asked to explain my use-case. I am trying to write a unit-test library (I know there is a Boost Test library for this purpose). And my aim is not to use any macros at all : struct my_test_case : public unit_test::test { void some_test() { assert_test(false, "test failed."); } }; My test suite

Member function pointer issue with standard library methods

淺唱寂寞╮ 提交于 2019-12-01 05:22:49
This question is spawned from Passing a member function pointer to an overloaded class method into a template function . You need not read that to understand this question. Probably both the questions will have the same answer. I am getting compiler error for below simple code . #include<set> template<typename Return, typename T> T ReceiveFuncPtr (Return (T::*Method)(const int&)) { T obj; // Found and declared an object of actual container class (obj.*Method)(1); // Some processing return obj; // Returned that container class object with RVO } int main () { ReceiveFuncPtr(&std::set<int>: