member-function-pointers

Is it safe to “upcast” a method pointer and use it with base class pointer?

て烟熏妆下的殇ゞ 提交于 2019-11-29 01:53:43
Let's say I have a pointer type that can hold the address of a base class method. Can I assign the address of a subclass method to it and expect it to work correctly? In my case I'm using it with a base class pointer and the dynamic type of the object is the derived class. struct B { typedef void (B::*MethodPtr)(); }; struct D: public B { void foo() { cout<<"foo"<<endl; } }; int main(int argc, char* argv[]) { D d; B* pb = &d; //is the following ok, or undefined behavior? B::MethodPtr mp = static_cast<B::MethodPtr>(&D::foo); (pb->*mp)(); } The standard says this when talking about static_cast:

boost::bind & boost::function pointers to overloaded or templated member functions

删除回忆录丶 提交于 2019-11-28 21:28:18
I have a callback mechanism, the classes involved are: class App { void onEvent(const MyEvent& event); void onEvent(const MyOtherEvent& event); Connector connect; } class Connector { template <class T> void Subscribe(boost::function <void (const T&)> callback); } App::App() { connect.Subscribe<MyEvent>(&App::OnEvent<MyEvent>); } First off this code doesn't compile, it's an illustration. The use of templates complicates my example, but I left them in because its affecting my problem. It seems certain to me that my subscribe needs to be templated because the Connector class doesn't know how many

C++ Pointer to virtual function

孤街醉人 提交于 2019-11-28 18:24:16
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? Pointers to members take into account the virtuality of the functions they point at. For example: #include <iostream> struct Base { virtual void f() { std::cout << "Base::f()" << std::endl; } }; struct Derived:Base { virtual void f() { std:

c++ member function pointer problem

∥☆過路亽.° 提交于 2019-11-28 12:11:24
I'm new to c++ . I want to know about object pointer and pointer to member function . I wrote a code which is following: code : #include <iostream> using namespace std; class golu { int i; public: void man() { cout<<"\ntry to learn \n"; } }; int main() { golu m, *n; void golu:: *t =&golu::man(); //making pointer to member function n=&m;//confused is it object pointer n->*t(); } but when i compile it it shows me two error which is following: pcc.cpp: In function ‘int main()’: pcc.cpp:15: error: cannot declare pointer to ‘void’ member pcc.cpp:15: error: cannot call member function ‘void golu:

C++ Comparing Member Function Pointers

我的梦境 提交于 2019-11-28 08:21:50
问题 In C++, is it possible to define a sort order for pointers to member functions? It seems that the operator< is undefined. Also, it's illegal to cast to void*. class A { public: void Test1(){} void Test2(){} }; int main() { void (A::* const one)() = &A::Test1; void (A::* const two)() = &A::Test2; bool equal = one == two; //Equality works fine. bool less = one < two; //Less than doesn't. return 0; } Thanks! 回答1: Function pointers are not relationally comparable in C++. Equality comparisons are

Cast member function for create_pthread() call

夙愿已清 提交于 2019-11-28 05:13:34
问题 I want to stop the warning server.cpp:823: warning: converting from 'void* (ClientHandler:: )()' to 'void ( )(void )' in the call: pthread_create(th, NULL, (void* (*)(void*)) &ClientHandler::handle, (void *) clientHandler); where handle() is a member function of ClientHandler : void* ClientHandler::handle(); I have difficulties deciphering the function-type message from the compiler. The question is: Should I change the handle() interface? Can I get rid of casting overall? Should I change the

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

C++: Function pointer to functions with variable number of arguments

泪湿孤枕 提交于 2019-11-27 22:51:22
I'm trying to figure out a way of how to be able to assign a function pointer to functions with different number of arguments. I have a while loop which takes a number of different functions as a conditional statement, so instead of writing multiple while loops with exactly the same code inside I'd like to have one with a function pointer. All the functions are of format bool f(...) . I think some code will best illustrate what I mean: int a, b, c, d; MyClass* my_class; typedef bool (MyClass::*my_fun_t)(); my_fun_t my_fun; if (condition1) my_fun = &MyClass::function_one(); else if (condition2)

Is it safe to “upcast” a method pointer and use it with base class pointer?

早过忘川 提交于 2019-11-27 21:45:25
问题 Let's say I have a pointer type that can hold the address of a base class method. Can I assign the address of a subclass method to it and expect it to work correctly? In my case I'm using it with a base class pointer and the dynamic type of the object is the derived class. struct B { typedef void (B::*MethodPtr)(); }; struct D: public B { void foo() { cout<<"foo"<<endl; } }; int main(int argc, char* argv[]) { D d; B* pb = &d; //is the following ok, or undefined behavior? B::MethodPtr mp =

Why the size of a pointer to a function is different from the size of a pointer to a member function?

帅比萌擦擦* 提交于 2019-11-27 20:04:52
Isn't a pointer just an address? Or I'm missing something? I tested with several types of pointers: pointers to any variables is the same (8B on my platform) pointers to functions are the same size, as pointers to variables (8B again) pointers to functions with different parameters - still the same (8B) BUT pointers to member functions are bigger - 16B on my platform. Three things: Why are pointers to member functions bigger? What more information do they need? As far as I know, the standard says nothing about the size of a pointer, except that void* must be able to "contain" any pointer type.