pointer-to-member

Using a member function pointer within a class

懵懂的女人 提交于 2019-11-30 07:28:31
Given an example class: class Fred { public: Fred() { func = &Fred::fa; } void run() { int foo, bar; *func(foo,bar); } double fa(int x, int y); double fb(int x, int y); private: double (Fred::*func)(int x, int y); }; I get a compiler error at the line calling the member function through the pointer "*func(foo,bar)", saying: "term does not evaluate to a function taking 2 arguments". What am I doing wrong? The syntax you need looks like: ((object).*(ptrToMember)) So your call would be: ((*this).*(func))(foo, bar); I believe an alternate syntax would be: (this->*func)(foo, bar); You need the

About shared_ptr and pointer to member operator `->*` and `std::bind`

ⅰ亾dé卋堺 提交于 2019-11-29 03:58:24
Recently I discovered that shared_ptr does not have pointer to member operator ->* . I created simple example: template <typename Pointer, typename Function, typename... Args> auto invoke1(Pointer p, Function f, Args... args) -> decltype((p->*f)(args...)) { return (p->*f)(args...); } struct A { void g() { std::cout << "A::g()\n"; } }; int main() { A a; invoke1(&a, &A::g); // works!! std::shared_ptr<A> sa = std::make_shared<A>(); invoke1(sa, &A::g); // compile error!! } Q1: Why is so? Why shared_ptr does not have this operator? I added such operator for shared_ptr and the example started to

Pointer-to-member confusion

为君一笑 提交于 2019-11-28 13:32:13
I'm trying to understand the consistency in the error that is thrown in this program: #include <iostream> class A{ public: void test(); int x = 10; }; void A::test(){ std::cout << x << std::endl; //(1) std::cout << A::x << std::endl; //(2) int* p = &x; //int* q = &A::x; //error: cannot convert 'int A::*' to 'int*' in initialization| //(3) } int main(){ const int A::* a = &A::x; //(4) A b; b.test(); } The output is 10 10 . I labelled 4 points of the program, but (3) is my biggest concern: x is fetched normally from inside a member function. x of the object is fetched using the scope operator

How to get the address of an overloaded member function?

你说的曾经没有我的故事 提交于 2019-11-28 09:46:21
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 example above. Any suggestion? EDIT Beware, the code above did not reflect my real-world problem, which

Pointer to class member as a template parameter

谁说胖子不能爱 提交于 2019-11-28 08:14:22
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 following, but the syntax is illegal: template <class Class, class Result> template <Result Class::*Member>

Pointer-to-member confusion

不打扰是莪最后的温柔 提交于 2019-11-27 19:29:27
问题 I'm trying to understand the consistency in the error that is thrown in this program: #include <iostream> class A{ public: void test(); int x = 10; }; void A::test(){ std::cout << x << std::endl; //(1) std::cout << A::x << std::endl; //(2) int* p = &x; //int* q = &A::x; //error: cannot convert 'int A::*' to 'int*' in initialization| //(3) } int main(){ const int A::* a = &A::x; //(4) A b; b.test(); } The output is 10 10 . I labelled 4 points of the program, but (3) is my biggest concern: x is

Strange C++ rule for member function pointers? [duplicate]

雨燕双飞 提交于 2019-11-27 16:01:16
问题 Possible Duplicate: Error with address of parenthesized member function In this recent question the OP ran into a strange provision of the C++ language that makes it illegal to take the address of a member function if that member function name is parenthesized. For example, this code is illegal: struct X { void foo(); }; int main() { void (X::* ptr)(); ptr = &(X::foo); // Illegal; must be &X::foo } I looked this up and found that it's due to §5.3.1/3 of the C++ ISO spec, which reads A pointer

Casting between void * and a pointer to member function

ぃ、小莉子 提交于 2019-11-27 15:04:15
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 call_int_function(lua_State *L) { // this next line is problematic void (T::*method)(int, int) = reinterpret_cast

How to print member function address in C++

社会主义新天地 提交于 2019-11-27 08:06:17
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 I print MyFunc 's address using std::cout ? I don't believe that there are any facilities provided by

Where are member functions stored for an object?

爱⌒轻易说出口 提交于 2019-11-27 04:26:33
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 int)(&((mystruct*)0)->c); unsigned int offset_from_start_structure2 = (unsigned int)(&((mystruct*)0)-