问题描述
- C++的mem_fun_ref如何支持多参数的类函数
- 今天想探究一下mem_fun_ref的用法。发现,对于无参数或者一个参数的类函数它可以建立引用,但两个或多个参数的函数呢,应该怎么建立引用呢?求高人指点。像下面代码那样,怎么给add函数建立引用呢?
#include <functional>#include <iostream>using namespace std;class Base {public: void say() { cout << ""hello"" << endl; } double pi() { return 3.1415926; } int twice(int x) { return (2 * x); } int add(int x int y) { return (x + y); }};int main() { mem_fun_ref_t<void Base> handle1 = mem_fun_ref(&Base::say); mem_fun_ref_t<double Base> handle2 = mem_fun_ref(&Base::pi); mem_fun1_ref_t<int Base int> handle3 = mem_fun_ref(&Base::twice); // mem_fun1_ref_t<int Base int int> handle4 = mem_fun_ref(&Base::add); Base b; handle1(b); cout << handle2(b) << endl; cout << handle3(b 4) << endl; // cout << handle4(b 4 3) << endl; return 0;}
时间: 2024-12-21 20:19:14