pointer-to-member

How to get the address of an overloaded member function?

北慕城南 提交于 2019-11-27 03:11:12
问题 I'm trying to get a pointer to a specific version of an overloaded member function. Here's the example: class C { bool f(int) { ... } bool f(double) { ... } bool example() { // I want to get the "double" version. typedef bool (C::*MemberFunctionType)(double); MemberFunctionType pointer = &C::f; // <- Visual C++ complains } }; The error message is "error C2440: 'initializing' : cannot convert from 'overloaded-function' to 'MemberFunctionType'" This works if f is not overloaded, but not in the

Pointer to class member as a template parameter

我们两清 提交于 2019-11-27 02:07:35
问题 I want to use a pointer to a class member as a template parameter as in: template <class Class, class Result, Result Class::*Member> struct MyStruct { // ... }; Using this struct like MyStruct<SomeClass, SomeResult, &SomeClass::value> variable works just fine, but I don't like that I have to specify SomeClass and SomeResult . I would like to use MyStruct<&SomeClass::value> variable if that is possible, but without losing the ability to pass any class and have any result type. I tried the

Error with address of parenthesized member function

拟墨画扇 提交于 2019-11-27 01:32:55
I found something interesting. The error message says it all. What is the reason behind not allowing parentheses while taking the address of a non-static member function? I compiled it on gcc 4.3.4. #include <iostream> class myfoo{ public: int foo(int number){ return (number*10); } }; int main (int argc, char * const argv[]) { int (myfoo::*fPtr)(int) = NULL; fPtr = &(myfoo::foo); // main.cpp:14 return 0; } Error: main.cpp:14: error: ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function. Say '&myfoo::foo' From the

Why doesn't reference-to-member exist in C++?

瘦欲@ 提交于 2019-11-26 22:38:44
In C++ I can chose between function pointers and function references (or even function values for the sake of completeness): void call_function_pointer (void (*function)()) { (*function) (); } void call_function_reference (void (&function)()) { function (); } void call_function_value (void function()) { function (); } When it comes to methods however, I don't seem to have this choice between pointers and references. template <class T> void call_method_pointer (T* object, void (T::*method)()) { (object->*method) (); } // the following code creates a compile error template <class T> void call

What are the Pointer-to-Member ->* and .* Operators in C++?

自作多情 提交于 2019-11-26 15:53:47
Yes, I've seen this question and this FAQ (wrong link) this FAQ , but I still don't understand what ->* and .* mean in C++. Those pages provide information about the operators (such as overloading), but don't seem to explain well what they are . What are ->* and .* in C++, and when do you need to use them as compared to -> and . ? Armen Tsirunyan I hope this example will clear things for you //we have a class struct X { void f() {} void g() {} }; typedef void (X::*pointer)(); //ok, let's take a pointer and assign f to it. pointer somePointer = &X::f; //now I want to call somePointer. But for

How to print member function address in C++

白昼怎懂夜的黑 提交于 2019-11-26 14:00:31
问题 It looks like std::cout can't print member function's address, for example: #include <iostream> using std::cout; using std::endl; class TestClass { void MyFunc(void); public: void PrintMyFuncAddress(void); }; void TestClass::MyFunc(void) { return; } void TestClass::PrintMyFuncAddress(void) { printf("%p\n", &TestClass::MyFunc); cout << &TestClass::MyFunc << endl; } int main(void) { TestClass a; a.PrintMyFuncAddress(); return EXIT_SUCCESS; } the result is something like this: 003111DB 1 How can

Where are member functions stored for an object?

我是研究僧i 提交于 2019-11-26 11:12:34
问题 I\'m experimenting with C++ to understand how class/structures and their respective objects are laid out in memory and I understood that each field of a class/structure is an offset into their respective object (so I can have a member variable pointer). I don\'t understand why, even if I can have member function pointers, the following code doesn\'t work: struct mystruct { void function() { cout << \"hello world\"; } int c; }; int main() { unsigned int offset_from_start_structure = (unsigned

Casting between void * and a pointer to member function

女生的网名这么多〃 提交于 2019-11-26 09:54:22
问题 I\'m currently using GCC 4.4, and I\'m having quite the headache casting between void* and a pointer to member function. I\'m trying to write an easy-to-use library for binding C++ objects to a Lua interpreter, like so: LuaObject<Foo> lobj = registerObject(L, \"foo\", fooObject); lobj.addField(L, \"bar\", &Foo::bar); I\'ve got most of it done, except for the following function (which is specific to a certain function signature until I have a chance to generalize it): template <class T> int

Error with address of parenthesized member function

心不动则不痛 提交于 2019-11-26 09:44:10
问题 I found something interesting. The error message says it all. What is the reason behind not allowing parentheses while taking the address of a non-static member function? I compiled it on gcc 4.3.4. #include <iostream> class myfoo{ public: int foo(int number){ return (number*10); } }; int main (int argc, char * const argv[]) { int (myfoo::*fPtr)(int) = NULL; fPtr = &(myfoo::foo); // main.cpp:14 return 0; } Error: main.cpp:14: error: ISO C++ forbids taking the address of an unqualified or

Why doesn&#39;t reference-to-member exist in C++?

亡梦爱人 提交于 2019-11-26 08:22:06
问题 In C++ I can chose between function pointers and function references (or even function values for the sake of completeness): void call_function_pointer (void (*function)()) { (*function) (); } void call_function_reference (void (&function)()) { function (); } void call_function_value (void function()) { function (); } When it comes to methods however, I don\'t seem to have this choice between pointers and references. template <class T> void call_method_pointer (T* object, void (T::*method)())