member-function-pointers

C++: Pointer to monomorphic version of virtual member function?

喜夏-厌秋 提交于 2019-11-27 04:20:26
问题 In C++, it's possible to get a pointer to a (non-static) member function of a class, and then later invoke it on an object. If the function was virtual, the call is dispatched dynamically depending on the dynamic type of the object. It's also possible (not using a member pointer) to call virtual member functions of objects monomorphically, by explicitly providing the scope containing the version to use. The following code demonstrates this: #include <iostream> using std::cout; using std::endl

Pointers to members representations

爱⌒轻易说出口 提交于 2019-11-26 21:17:24
问题 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? 回答1: Danny Kalev explains this quite

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

我们两清 提交于 2019-11-26 21:14:10
问题 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:

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

穿精又带淫゛_ 提交于 2019-11-26 19:51:12
问题 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

How to hash and compare a pointer-to-member-function?

你离开我真会死。 提交于 2019-11-26 19:08:51
How can i hash (std::tr1::hash or boost::hash) a c++ pointer-to-member-function? Example: I have several bool (Class::*functionPointer)() (not static) that point to several diferent methods of the class Class and i need to hash those pointer-to-member-function. How can i do that? Also how can i compare (std::less) those member function pointers so i can store them in a std::set? All C++ objects, including pointers to member functions, are represented in memory as an array of chars. So you could try: bool (Class::*fn_ptr)() = &Class::whatever; const char *ptrptr = static_cast<const char*>

C++ inheritance and member function pointers

99封情书 提交于 2019-11-26 18:11:30
问题 In C++, can member function pointers be used to point to derived (or even base) class members? EDIT: Perhaps an example will help. Suppose we have a hierarchy of three classes X , Y , Z in order of inheritance. Y therefore has a base class X and a derived class Z . Now we can define a member function pointer p for class Y . This is written as: void (Y::*p)(); (For simplicity, I'll assume we're only interested in functions with the signature void f() ) This pointer p can now be used to point

How do I call a pointer-to-member-function?

筅森魡賤 提交于 2019-11-26 14:16:50
问题 I'm getting a compile error (MS VS 2008) that I just don't understand. After messing with it for many hours, it's all blurry and I feel like there's something very obvious (and very stupid) that I'm missing. Here's the essential code: typedef int (C::*PFN)(int); struct MAP_ENTRY { int id; PFN pfn; }; class C { ... int Dispatch(int, int); MAP_ENTRY *pMap; ... }; int C::Dispatch(int id, int val) { for (MAP_ENTRY *p = pMap; p->id != 0; ++p) { if (p->id == id) return p->pfn(val); // <--- error

How to hash and compare a pointer-to-member-function?

徘徊边缘 提交于 2019-11-26 06:48:45
问题 How can i hash (std::tr1::hash or boost::hash) a c++ pointer-to-member-function? Example: I have several bool (Class::*functionPointer)() (not static) that point to several diferent methods of the class Class and i need to hash those pointer-to-member-function. How can i do that? Also how can i compare (std::less) those member function pointers so i can store them in a std::set? 回答1: All C++ objects, including pointers to member functions, are represented in memory as an array of chars. So