member-function-pointers

C++ class member function and callback from C API

我只是一个虾纸丫 提交于 2019-12-05 14:19:40
I am trying to learn how to call this write_data(…) function from the funmain() function in the class as shown in the code bellow. (I know this program works if I just list these two functions without putting it inside a class). curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data) line gives me error and wouldn’t let me call the write_data(…) function. Can you please correct my code and tell me how I can achieve this. Any help would be greatly appreciated. Thanks. error C3867: 'go_website::write_data': function call missing argument list; use '&go_website::write_data' to create a pointer

Weird pointer to member function syntax

社会主义新天地 提交于 2019-12-05 13:55:49
I understand how to declare the type of a function: typedef void (typedef_void_f)(); // typedef_void_f is void() using alias_void_f = void(); // alias_void_f is void() And it can be used to declare function pointers: void function() { std::cout << __PRETTY_FUNCTION__ << '\n'; } typedef_void_f *a = function; // pointer to void() alias_void_f *b = function; // pointer to void() For member function pointers the syntax is slightly more complicated: struct S { void function() { std::cout << __PRETTY_FUNCTION__ << '\n'; } }; typedef void (S::*typedef_void_m_f)(); using alias_void_m_f = void (S::*)()

Member function pointer

丶灬走出姿态 提交于 2019-12-05 11:29:07
问题 If the following from the C++ FAQ Lite is true: "a function name decays to a pointer to the function" (as an array name decays to a pointer to its first element); why do we have to include the ampersand? typedef int (Fred::*FredMemFn)(char x, float y); FredMemFn p = &Fred::f; And not just: typedef int (Fred::*FredMemFn)(char x, float y); FredMemFn p = Fred::f; In the second case Fred::f is a function and can decay to a pointer to that function. I hope this question is not that stupid. 回答1:

Function member pointer with private base

别来无恙 提交于 2019-12-05 08:25:44
The following code yields a compile time error: ' base::print ' : cannot access private member declared in class ' base_der ' However, I have made the member public in the derived class. Why doesn't this work? #include <iostream> using namespace std; class base { public: int i; void print(int i) { printf("base i\n"); } }; class base_der : private base { public: using base::print; }; int main() { // This works: base_der cls; cls.print(10); // This doesn't: void (base_der::* print)(int); print = &base_der::print; // Compile error here } I think there are a few interacting problems contributing

Function taking both pointer to member-function and pointer to const member-function

一世执手 提交于 2019-12-05 08:21:37
I have the following code base: template <typename Type> class SomeClass { public: template <typename ReturnType, typename... Params> void register_function(const std::pair<std::string, ReturnType (Type::*)(Params...)> fct) { auto f = [fct](Params... params) -> ReturnType { return (Type().*fct.second)(std::ref(params)...); } // ... } }; This works when I pass a pointer to a member-function (non-const). However, if I want to pass a pointer to a const member-function, it results in a compile error and I must duplicate the above function to get this code: template <typename Type> class SomeClass

C++0x lambda wrappers vs. bind for passing member functions

不羁岁月 提交于 2019-12-05 01:08:17
This is basically a question about the readability, style, performance of 2 different approaches to creating/passing a functor that points to a member method from within a class constructor/method. Approach 1: using namespace std::placeholders; std::bind( &MyClass::some_method, this, _1, _2, _3 ) Approach 2: [ this ](const arg1& a, arg2 b, arg3& c) -> blah { some_method( a, b, c ); } I was wondering if using the lambda is just gratuitous in this situation, in some respects it is easier to see what is going on, but then you have to explicitly provide the arg types. Also i prefer not to have

How do you pass a function of a class as a parameter to another function of the same class

ε祈祈猫儿з 提交于 2019-12-04 18:19:57
i basically want to use a dif function to extract a different element of a class (ac). the code is similar to this: .h: class MyClass { public: double f1(AnotherClass &); void MyClass::f0(AnotherClass & ac, double(MyClass::*f1)(AnotherClass &)); }; .cc: double MyClass::f1(AnotherClass & ac) { return ac.value; } void MyClass::f0(AnotherClass & ac, double(MyClass::*f1)(AnotherClass &)) { std::cout << f1(ac); } didn't work, it gives error#547 "nonstandard form for taking the address of a member function" EDIT: I call it from: void MyClass(AnotherClass & ac) { return f0(ac,&f1); // original and

C++ member function pointer, boost::signals

混江龙づ霸主 提交于 2019-12-04 16:49:56
I have the following situation, (better in code) class Foo { private: typedef boost::signal<void ()> Signal; Signal signal; public: void Register_SignalFunction(const Signal::slot_type& slot); void Unregister_SignalFunction(const Signal::slog_type& slot); } class OtherFoo { Foo foo; public: OtherFoo() { foo.Register_SignalFunction(&OnSignal) //I know I can't do this that is precisely my question. } void OnSignal(); //what to do when signal fires } So the question is, how i pass a 'member-function' pointer to the Register method. Also, is this ok? What I want/need, is some sort of delegate

Pointer to function to member function

≡放荡痞女 提交于 2019-12-04 12:26:32
I want to use a library (nlopt) that has a function set_min_objective which takes a pointer to a numerical function myfunc and find its minimum. I would like to create a class that will contain a suitably initialized member function. set_min_objective would then find an optimum in a specific instance (myP in the example below). The call sequence is: opt.set_min_objective(myfunc, NULL); and I would like to use something like: opt.set_min_objective(myP.f, NULL); the error I get when compiling this is: main.cpp: In function 'int main()': main.cpp:79:34: error: no matching function for call to

How to register a derived class member function pointer with a base class

蓝咒 提交于 2019-12-04 09:27:37
问题 As opposed to virtual member functions, I need a solution where a function implemented at each level class derivation can be registered for later call by the base class. ( Not just the most derived implementation) To do this, I was thinking on providing a mechanism for derived classes to register their function with the base class such as during the derived class constructor. I'm having trouble with the member function pointer argument though. I was thinking that Derived is derived from Base,