member-function-pointers

To pass a pointer to a member function

家住魔仙堡 提交于 2019-12-01 03:43:12
问题 I have an class with instance functions (or methods?). From within an instance, I try to pass pointers to those functions to a library. The library expects static functions. When I pass my pointers to the callback functions, the compiler complains that my functions are not static. I tried to put make them static, but if I do so, then I can't access the instance fields from within the functions. How could I go around this? Similar question is : Using a C++ class member function as a C callback

Protected member function address in derived class is not accessible

烂漫一生 提交于 2019-12-01 03:26:52
#include <iostream> class A { protected: void foo() {} }; class B : public A { public: void bar() { std::cout << (&A::foo) << std::endl; } }; int main() { B b; b.bar(); } Here I am trying to get address of protected member function of base class. I am getting this error. main.cpp: In member function ‘void B::bar()’: main.cpp:5: error: ‘void A::foo()’ is protected main.cpp:13: error: within this context make: *** [all] Error 1 Changing foo to public works. Also printing &B::foo works. Can you please explain why we can't get address of protected member function of base class? B is allowed to

How do I get the member function pointer of a destructor?

[亡魂溺海] 提交于 2019-11-30 17:27:45
问题 Assume I have struct X { ~X() {} }; What's the type of and how do I get the member function pointer of X::~X() in C++03? I don't want to actually call it, just use in SFINAE to figure if there exists a destructor for a given type. 回答1: You can't get the function pointer of a destructor nor a constructor. Nevertheless a destructor always exist for a type, and you can't detect if its private with as access specifiers are not considered by SFINAE . On the subject of invoking what would be the

How to best pass methods into methods of the same class

天涯浪子 提交于 2019-11-30 13:53:28
I have this C++ class that one big complicated method compute that I would like to feed with a "compute kernel", a method of the same class. I figure I would do something along the lines of class test { int classVar_ = 42; int compute_add(int a, int b) { compute(int a, int b, this->add_()) } int compute_mult(int a, int b) { compute(int a, int b, this->mult_()) } int compute_(int a, int b, "pass in add or multiply as f()") { int c=0; // Some complex loops { c += f(a,b) // } return c; } int add_(int a, int b){a+b+classVar_;} int multiply_(int a, int b){a*b+classVar_;} ... } but I'm not sure how

C++ pointer-to-method template deduction doesn't compile when targeting x86, but works with x64

眉间皱痕 提交于 2019-11-30 04:44:14
问题 I've got this sample code: struct A { int foo() { return 27; } }; template<typename T> struct Gobstopper { }; template<> struct Gobstopper<int(void)> { Gobstopper(int, int) { } // To differentiate from general Gobstopper template }; template<typename ClassType, typename Signature> void DeduceMethodSignature(Signature ClassType::* method, ClassType& instance) { // If Signature is int(), Gobstopper<> should resolve to the specialized one. // But it only does on x64! Gobstopper<Signature>(1, 2);

C++ Comparing Member Function Pointers

我只是一个虾纸丫 提交于 2019-11-29 14:46:37
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! Function pointers are not relationally comparable in C++. Equality comparisons are supported, except for situations when at least one of the pointers actually points to a virtual member

Why must I use address-of operator to get a pointer to a member function?

落爺英雄遲暮 提交于 2019-11-29 14:06:41
struct A { void f() {} }; void f() {} int main() { auto p1 = &f; // ok auto p2 = f; // ok auto p3 = &A::f; // ok // // error : call to non-static member function // without an object argument // auto p4 = A::f; // Why not ok? } Why must I use address-of operator to get a pointer to a member function? auto p1 = &f; // ok auto p2 = f; // ok The first is more or less the right thing. But because non-member functions have implicit conversions to pointers, the & isn't necessary. C++ makes that conversion, same applies to static member functions. To quote from cppreference : An lvalue of function

Cast member function for create_pthread() call

橙三吉。 提交于 2019-11-29 12:09:30
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 cast? To what exactly? Something completely different? Mat You can't do that directly, pointers to

What are the rules for function pointers and member function pointers to Standard functions?

*爱你&永不变心* 提交于 2019-11-29 04:29:00
What are the existing rules for taking function pointers or member function pointers to Standard functions? For example, something like auto p = &std::string::size; Is this legal? Would it be more or less legal if I explicitly requested the correct type, so it would function even if there was an additional implementation-added overload of std::string::size ? Using the "correct" type doesn't make things better: Except for the virtual functions all functions in the standard C++ library can have additional arguments as long as these are defaulted. Since the functions can also be declared with

Object-Oriented Callbacks for C++?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-29 02:13:37
Is there some library that allows me to easily and conveniently create Object-Oriented callbacks in c++? the language Eiffel for example has the concept of "agents" which more or less work like this: class Foo{ public: Bar* bar; Foo(){ bar = new Bar(); bar->publisher.extend(agent say(?,"Hi from Foo!", ?)); bar->invokeCallback(); } say(string strA, string strB, int number){ print(strA + " " + strB + " " + number.out); } } class Bar{ public: ActionSequence<string, int> publisher; Bar(){} invokeCallback(){ publisher.call("Hi from Bar!", 3); } } output will be: Hi from Bar! 3 Hi from Foo! So - the