member-function-pointers

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

萝らか妹 提交于 2020-01-13 06:47:47
问题 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

Is possible to fix the iostream cout/cerr member function pointers being printed as 1 or true?

本小妞迷上赌 提交于 2020-01-13 05:21:26
问题 If you run the following: #include <iostream> int main() { std::cout.setf(std::ios::boolalpha); std::cout << &main << "\n"; std::cout << (void*)&main << "\n"; // The workaround return 0; } // prints something like // true // 0x55deee04189a If you remove the std::cout.setf(std::ios::boolalpha) call, it just prints 1 instead of true . If you look at the https://godbolt.org/z/6CFH3P assembly, you will notice that the C++ template resolution is choosing the boolean operator std::basic_ostream

How to call a function using pointer-to-member-function

感情迁移 提交于 2020-01-11 11:25:07
问题 I have a class: class A { void test_func_0(int); void run(); typedef void(A::*test_func_t)(int); struct test_case_t{ test_func_t test_func; } test_case[100]; }; Now I want to call test_func() inside run(): void A::run() { test_case[0].test_func = &test_func_0; test_case[0].*(test_func)(1); } The last line of my code, doesn't work(compile error), no matter what combination I try. 回答1: Use this: void A::run() { test_case[0].test_func = &A::test_func_0; (this->*(test_case[0].test_func))(1); }

Passing member function to another object's member function C++

人走茶凉 提交于 2020-01-04 23:45:55
问题 I am having issues trying to pass a function as an argument in another object's function. I am well aware there are many similar topics but I either can't get their solution to work or can't understand them. class foo { public: void func1(void (*Drawing)(void)); template<class T> void func2(void (T::*Drawing)(void)); }; class bar { private: foo myFoo; void Drawing(); void func3() { // Attempt 1 myFoo.func1(Drawing); // Attempt 2 myFoo.func2<bar>(&bar::Drawing); } }; So in my first attempt, I

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

假如想象 提交于 2020-01-02 01:06:27
问题 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

Passing member function pointer to member object in c++

有些话、适合烂在心里 提交于 2019-12-28 05:10:33
问题 I have a problem with using a pointer to function in C++. Here is my example: #include <iostream> using namespace std; class bar { public: void (*funcP)(); }; class foo { public: bar myBar; void hello(){cout << "hello" << endl;}; }; void byebye() { cout << "bye" << endl; } int main() { foo testFoo; testFoo.myBar.funcP = &byebye; //OK testFoo.myBar.funcP = &testFoo.hello; //ERROR return 0; } Compilator returns an error at testFoo.myBar.funcP = &testFoo.hello; : ISO C++ forbids taking the

Refactor code that violates “Needless Repition” principle

随声附和 提交于 2019-12-25 06:27:28
问题 Assume class Animal which does Eat, Sleep and Make Noise. Assume class Mammal : public Animal which also does MakesBaby Assume that Mammal also does Eat, Sleep and Make Noise which it inherits from Animal. This code is extended from code presented in this question which was answered yesterday by Dan Masek. The problems with this code are: Mammal can't inherit from Animal (compile error) Mammal cannot Eat, Sleep or Make Noise Code suffers from "Needless Repetition", thus meeting with the scorn

Address of pointer “this” changed unexpectedly inside a pointer to member function call

感情迁移 提交于 2019-12-25 01:25:43
问题 I have problem with the pointer to member function call. The address of pointer "this" outside the function pointer call is different than inside the call thus all access to the class variables results wrong values. I include the code here. class ClassInterface { public: ClassInterface(void); ~ClassInterface(void); }; class ClassA:public ClassInterface { public: float _a; public: ClassA(void); ~ClassA(void); virtual void Update(); }; class ClassB:public ClassA { public: ClassB(void); ~ClassB

How to create a vector filled with C-Style function pointers and lambdas(with and without captures)

社会主义新天地 提交于 2019-12-24 19:04:27
问题 Ive been learning about lambdas and function pointers lately and wanted to use them in a simple callback system. The map that stores an event and all callbacks that should be called when it gets triggered looks like this: std::unordered_map< sf::Event::EventType , std::vector< Callback > > eventMap; . I defined Callback this way typedef void(*Callback)(sf::Event const&); . My registerEvent() function takes an event and a Callback as arguments. registerEvent() can then be called this way:

C++. Calling a virtual member function in destructor [duplicate]

六眼飞鱼酱① 提交于 2019-12-24 11:43:50
问题 This question already has answers here : Calling virtual function from destructor (5 answers) Closed 4 years ago . Every class that gets extended with this calls abort in the destructor and the call stack tells me that the function that called abort was called from a unreasonable spot in the header file. The other functions get overriden and work fine though. Renderable.h #pragma once class Renderable { public: virtual void update(long delta) = 0; virtual void destroy() = 0; virtual void