member-function-pointers

C++ Pointer to virtual function

霸气de小男生 提交于 2019-12-17 23:29:50
问题 If you have a struct like this one struct A { void func(); }; and a reference like this one A& a; you can get a pointer to its func method like this: someMethod(&A::func); Now what if that method is virtual and you don't know what it is at run-time? Why can't you get a pointer like this? someMethod(&a.func); Is it possible to get a pointer to that method? 回答1: Pointers to members take into account the virtuality of the functions they point at. For example: #include <iostream> struct Base {

why can't I cast a pointer to Derived class member function to the same but of class Base?

冷暖自知 提交于 2019-12-17 16:48:21
问题 To me it looks perfectly safe to cast a void(Derived::*)() to a void(Base::*)() , like in this code: #include <iostream> #include <typeinfo> using namespace std; struct Base{ void(Base::*any_method)(); void call_it(){ (this->*any_method)(); } }; struct Derived: public Base{ void a_method(){ cout<<"method!"<<endl; } }; int main(){ Base& a=*new Derived; a.any_method=&Derived::a_method; a.call_it(); } But the compiler complains about the cast at a.any_method=&Derived::a_method; . Is this a

How to invoke pointer to member function when it's a class data member?

拜拜、爱过 提交于 2019-12-17 16:18:24
问题 struct B { void (B::*pf)(int, int); // data member B () : pf(&B::foo) {} void foo (int i, int j) { cout<<"foo(int, int)\n"; } // target method }; int main () { B obj; // how to call foo() using obj.pf ? } In above test code, pf is a data member of B . What's the grammar rule to invoke it ? It should be straight forward, but I am not getting a proper match. e.g. If I try obj.*pf(0,0); then I get: error: must use ‘.*’ or ‘->*’ to call pointer-to-member function in ‘pf (...)’, e.g. ‘(... ->* pf)

Pointers to virtual member functions. How does it work?

半世苍凉 提交于 2019-12-17 11:00:48
问题 Consider the following C++ code: class A { public: virtual void f()=0; }; int main() { void (A::*f)()=&A::f; } If I'd have to guess, I'd say that &A::f in this context would mean "the address of A's implementation of f()", since there is no explicit seperation between pointers to regular member functions and virtual member functions. And since A doesn't implement f(), that would be a compile error. However, it isn't. And not only that. The following code: void (A::*f)()=&A::f; A *a=new B; //

How Can I Pass a Member Function to a Function Pointer?

こ雲淡風輕ζ 提交于 2019-12-17 09:49:17
问题 class Child; class Parent { public: void (*funcPointer)(); void (*funcPointer2)(Parent* _this); void (Child::*funcPointer3)(); }; class Child: public Parent { public: void TestFunc(){ } void Do(){ Parent p; p.funcPointer=TestFunc; // error, '=': cannot convert from 'void (__thiscall Child::* )(void)' to 'void (__cdecl *)(void)' p.funcPointer2=TestFunc; // error too, '=': cannot convert from 'void (__thiscall Child::* )(void)' to 'void (__cdecl *)(Parent *)' p.funcPointer3=TestFunc; //this

Error in using unique_ptr with member function pointer

橙三吉。 提交于 2019-12-13 13:06:44
问题 I have a class as below class A { public: A(int key) : m_key(key) {} int Key() const {return m_key;} private: int m_key; }; I test using unique_ptr with member function pointer int (A::*MemFun)() const; MemFun = &A::Key; ( std::unique_ptr<A>(new A(10)) ->*MemFun ) (); // Error C2296 ( std::unique_ptr<A>(new A(10)).get() ->*MemFun ) (); // okay (*std::unique_ptr<A>(new A(10)) .*MemFun ) (); // okay The first one gives a compilation error (VC2010 gives error C2296, illegal, left operator

Templated Function Pointer in C++

随声附和 提交于 2019-12-13 07:49:48
问题 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;

How do I call a member function pointer?

旧城冷巷雨未停 提交于 2019-12-13 07:47:05
问题 I am using C++11 with GNU tool chain on Ubuntu 12.04 LTS 32 bit. I know similar questions have been asked, but I haven't found exactly this one, and I wasn't able to get anything that worked from what is already posted, so here goes: I have code like this: enum class FUNC_TYPES { FUNCTYPE_1,FUNCTYPE_2,FUNCTYPE_3}; class AClass { ... typedef void (AClass::* TFunc )( ); std::map<FUNC_TYPES, TFunc> mFuncMap; void doStuff(); .... } I initialize mFuncMap like this: void AClass::initFuncMap( ) {

C++ execute function with class from array

99封情书 提交于 2019-12-13 07:01:55
问题 I need to be able to call a function that is a member of a class by looking up the function pointer in an array. This class will have sub classes that do the same thing but call the parent class if they cannot fund the function. To make matters simple, I have cut out most of the code. What remains is shown below. The ultimate test is to create: 1) Mammal : public Animal 1.1) Cat : public Mammal 1.2) Dog : public Mammal 2) Reptile : public Animal 2.1) Bird : public Reptile I want to build this

How to pass a member function as a parameter to a function that doesn't expect it?

安稳与你 提交于 2019-12-13 04:58:33
问题 Say I have a function foo : void foo(void (*ftn)(int x)) { ftn(5); } It needs as a parameter a void function that accepts an int as a parameter. Consider void func1(int x) {} class X { public: void func2(int x) {} }; Now foo(&func1) is ok. But foo(&X::func2) isn't ok because X::func2 isn't static and needs a context object and its function pointer type is different. I tried foo(std::bind(&X:func2, this)) from inside X but that raises a type mismatch too. What is the right way of doing this?