Usage of class member functions in templates
In practice, many templates are used, and class member functions or variables are used as template parameters. Here we will give a small example. The code is very simple, but it is very common and practical in practice.
// I am only a test class. class TestClass {public: void testFunc (int val) {} double testFunc2 (double val) {return 0 ;}public: int m_testVal ;}; // It will also be used. If it is not a template, typedef void (TestClass: * test_func) (int) is quite practical. // if it is not about boost, do not talk about lambda, only the template is mentioned. // the template of the member function.
Void test_call (_ Tp * obj, _ Ret (_ Tp: * _ func) (_ Param), _ Param _ p) {(obj-> * _ func) (_ p);} // The member variable, which is rarely used. The template is mainly used in Imp.
Void test_member (_ Tp * obj, _ Param _ Tp: * value, _ Param _ p) {obj-> * value = _ p;} int main (int argc, char * argv []) {TestClass * tt = new TestClass; test_call
(Tt, & TestClass: testFunc, 6); test_call
(Tt, & TestClass: testFunc2, 6.9); // test_func f1 = & TestClass: testFunc; (tt-> * f1) is commonly used in non-template scenarios) (7); // use test_member (tt, & TestClass: m_testVal, 5); delete tt; return 1;} for member variables ;}
There is no extra explanation, and the code is often quite simple.