member-function-pointers

Function/Method pointers Pushed to a Deque

三世轮回 提交于 2019-12-13 01:05:41
问题 I am making a Queue for running functions. I put the functions that require being called into a std::deque<bool(*)()> Then I later on cycle through the deque calling each function and letting it run, sometimes even doing things based on the return. The problem I am having is actually with regards to placing these functions inside of the deque. I have this deque inside a class called A2_Game . I also have a class called Button . My code resembles the following: class Button { bool DoInput(); }

Template wrapper for const and non const member functions of arbitrary classes

牧云@^-^@ 提交于 2019-12-12 12:05:33
问题 I want to have a templated class (wrapper), which can take all possible classes (T) and do stuff (here evaluate) with the member functions of these classes (function). I found similar requests, which you can see here and here, but neither could satisfy the two conditions below. Conditions: Both, a pointer to the instance of the class ( T * ptr) and a pointer to the member function (function) must be accessible within the wrapper class. The wrapper class shall work with both, const and non

Determine type from non-type template parameter

拈花ヽ惹草 提交于 2019-12-12 11:32:59
问题 I wish I could do this: template <typename T x> struct Test { T val{x}; }; int main() { Test<3> test; return test.val; } But I can't. Right? I was answering a question here and I use the following template: template <typename T, typename V, typename VP, V(T::*getf)(), void (T::*setf)(VP)> Each of the types are manually specified. But that is a duplication because T , V and VP are already contained in the pointers to member functions getf and setf 's types. But if I try a template with only

Assigning C++ function pointers to member functions of the same object

廉价感情. 提交于 2019-12-12 11:05:58
问题 How do I get the function pointer assignments (and maybe the rest) in test.calculate to work? #include <iostream> class test { int a; int b; int add (){ return a + b; } int multiply (){ return a*b; } public: int calculate (char operatr, int operand1, int operand2){ int (*opPtr)() = NULL; a = operand1; b = operand2; if (operatr == '+') opPtr = this.*add; if (operatr == '*') opPtr = this.*multiply; return opPtr(); } }; int main(){ test t; std::cout << t.calculate ('+', 2, 3); } 回答1: There are

Able to use pointer to function to call private method of an external class

瘦欲@ 提交于 2019-12-12 10:29:42
问题 Based on the following answer to a recent question, I'm able to use a function pointer in order to call the private method Foo<T>::foo() from another class Bar , as shown below (see also ideone) #include <iostream> template<typename T> struct Bar { typedef void (T::*F)(); Bar( T& t_ , F f ) : t( t_ ) , func( f ) { } void operator()() { (t.*func)(); } F func; T& t; }; template<typename T> class Foo { private: void foo() { std::cout << "Foo<T>::foo()" << std::endl; } public: Foo() : bar( *this

Templated Function Pointer in C++

笑着哭i 提交于 2019-12-12 07:06:35
问题 I am facing a problem with templated member function pointer. The code is as shown below. #include <String> #include <iostream> template<typename T> struct method_ptr { typedef void (T::*Function)(std::string&); }; template <class T> class EventHandler { private: method_ptr<T>::Function m_PtrToCapturer; }; e:\EventHandler.h(13) : error C2146: syntax error : missing ';' before identifier 'm_PtrToCapturer' I am facing this error. Even If I use method_ptr<EventHandler>::Function m_PtrToCapturer;

Template Member Function Pointers

自古美人都是妖i 提交于 2019-12-12 05:31:13
问题 I currently have a problem, VS2010 nags that "TFunctionPointer" used in the constructor of "Nuke" & as datamember is undefined. Could someone please explain to me why this is ? Any help is very much appreciated. template<typename T> typedef void (T::* TFunctionPointer)(); class Nuke { public: Nuke( TFunctionPointer pFunction ); virtual ~Nuke(); private: TFunctionPointer m_pFunction; }; // EDIT What I'm trying to do is allow a function pointer to any type of class to be stored and called on

How to pass class member functions to a method in a 3rd party library?

柔情痞子 提交于 2019-12-12 05:23:09
问题 The following is the constructor of a class I would like to use in a 3rd party library (so altering this function isn't an option). template <class Space> moveset<Space>::moveset(particle<Space> (*pfInit)(rng*), void (*pfNewMoves)(long, particle<Space> &,rng*), int (*pfNewMCMC)(long,particle<Space> &,rng*)) However, rather than simply defining 3 global functions, I need each of the functions to know various extra information, which obviously I can't pass as there are no input arguments. To

c++ function ptr in unorderer_map, compile time error

风流意气都作罢 提交于 2019-12-12 01:23:18
问题 i am trying to implements an unorderer_map that implements as mapped_type, i was watching some examples that implements these but i cannot make it work. here is the code: #include<string> #include <unordered_map> namespace Test { class Example { public: Example() { auto aPair=std::make_pair("one",&Example::procesString); map.insert(aPair); } void procesString(std::string & aString) { } void processStringTwo(std::string & aString) { } typedef void(*fnPtr)(std::string &); std::unordered_map<std

Member function to a list of pointer

主宰稳场 提交于 2019-12-11 11:18:25
问题 Thanks for giving comments to the following. Class1 { debug(std::ostream&){} }; int main() { std::vector<Class1*> list1; // some work to do } Target Platform: Platform(1): Win 7x64, VS2010 Platform(2): Linux x32, g++ 4.4 Q: What should be the correct way to pass "std::cout" to following statement? std::for_each(list1.begin(), list1.end(), "afunction(&Class1::debug, std::cout)"); I previously used "std::cout" inside the debug() function, but later consider to give flexibility for the output of