Usage of class member functions in templates

Source: Internet
Author: User

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.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.