将函数名称发送给函数

时间:2013-09-22 13:47:12

标签: c++ function pointers arguments

我想做的是:

class A
{
    public:
    double sum(double a, double b);
    double max(double a, double b);
}

template <typename T>
class B
{
    std::vector<T> data;

    public:

    double sum (double a, double b);
    double max (double a, double b);
    double average( MyFunction, double a, dobule b)
    {
        double sum = 0;
        int n = data.size();

        for ( int i = 0; i < n; i++)
            sum = sum + data[i].MyFunction(double a, double b)

            return sum / n;
    }

}

示例:

double average( max, double a, double b)
{
    double sum = 0;
    int n = data.size();

    for ( int i = 0; i < n; i++)
        sum = sum + data[i].max(double a, double b)

        return sum / n;
}

为什么?

  1. 它会省去我写的功能:总和的平均值。平均最大值最小平均值 这些都是非常相似的功能。
  2. 编码方式B< B< A> >正常工作
  3. 我尝试了什么?

    • 函数指针
    • S1

        typedef double (A::*MyFunctionf)(double, double);
        typedef double (B<A>::*MyFunctionff)(double, double);
        typedef double (B<B<A> >::*MyFunctionfff)(double, double);
      
      • 它有效。问题:
        • 声明3-4 typedef函数指针
        • 并不美观
        • 如果我想在B内写入发送函数指针的函数,它将被硬编码,并且只有3个typedef中的1个可以被硬编码。含义:它不适用于所有情况
    • S2 (基于Template typedefs - What's your work around?):

        template <typename rtype, typename t>
        struct CallTemplate
        {
          typedef rtype (t::*ptr)(double, double);
        };
      
       // the function becomes :
       template <typename T>
       template<typename rtype>
       double B<T>::average(CallTemplate<double, T> MyFunction, double a, double b)
       {
          double sum = 0;
         int n = data.size();
      
            for ( int i = 0; i < n; i++)
              sum = sum + (data[i].*MyFunction)( a, b)
      
          return sum / n;
        }
      

      示例:

       // makes an error "Myfunction was not declared" + " 
       // dependent-name'{anonymous}::CallTemplate<double, A>::ptr' 
       // is parsed as a non-type, but instantiation yields a type"
       CallTemplate<double, A>::ptr MyFunction = &A::max; 
       Average(max, t, v);
      

    我不知道问题出在哪里。 我也试过Boost.Function

3 个答案:

答案 0 :(得分:2)

是的,这是可能的。

您正在寻找会员指针。然而,语法并不明显:

struct A
{
    double x, y;
    A(double x, double y) : x(x), y(y) {}
    double sum() { return x + y; }
    double max() { return std::max(x, y); }
};

这是一个定义了几种方法的类(summax)。

template <typename T>
struct B
{
    std::vector<T> data;
    double average(double (T::*MyMethod)())
    {
        double sum = 0;
        int n = data.size();
        for (int i = 0; i < n; i++)
            sum = sum + (data[i].*MyMethod)();
        return sum / n;
    }
};

这是一个接受方法指针的方法的类,它将计算在向量元素上调用指向方法的结果的平均值。

传递两个A方法的示例是:

int main(int argc, const char *argv[]) {
    B<A> b;
    b.data.push_back(A(1, 2));
    b.data.push_back(A(3, 4));
    b.data.push_back(A(5, 6));
    std::cout << b.average(&A::max) << std::endl;
    std::cout << b.average(&A::sum) << std::endl;
    return 0;
}

答案 1 :(得分:0)

使用Boost.Function解决方案

class A
{
    public:
    double sum(double a, double b);
    double max(double a, double b);
}

template <typename T>
class B
{
    std::vector<T> data;

    public:

    double sum (double a, double b);
    double max (double a, double b);
    double average(boost::function<double (T*, double, double) MyFunction, double a, dobule b)
    {
        double sum = 0;
        int n = data.size();

        for ( int i = 0; i < n; i++)
            sum = sum + MyFunction(&data[i], a, b)

            return sum / n;
    }

}

示例

boost::function<double (A*, a, b)> MyFunction = &A::max;
average(MyFunction, a, b);

工作示例::

#include <cmath>

using namespace std;

#include <vector>
#include <boost/function.hpp>

#define CallTemplate(returnvalue, FINClass) boost::function<returnvalue (FINClass*, double, double)>

class zc
{
    double n;
    double r;
    double p;

    public:
    zc(double nn, double rr, double pp):n(nn), r(rr), p(pp) {}

    double pv(double t, double v) { return p/ pow ( ( 1 + r + v), ( n - t) );}
    double d(double t, double v)  { return (n - t); }
    double c(double t, double v)  { return (n - t)*(n - t)+(n - t); }
};


template <typename T>
class Master
{
    public:

    Master(){}
    std::vector<T> data;

    double pv(double t, double v) {CallTemplate(double, T) f = &T::pv;  return sum(f, t, v);}
    double d(double t, double v)  {CallTemplate(double, T) f = &T::d;   return weightedAverage(f, t, v);}
    double c(double t, double v)  {CallTemplate(double, T) f = &T::c;   return weightedAverage(f, t, v);}

    double sum(CallTemplate(double, T) MyFunction, double t, double v)
    {
        double sum = 0;
        for( int i = 0, n = data.size(); i < n; i++)
            sum = sum + MyFunction(&data[i], t, v);

        return sum;
    }

    double weightedAverage(CallTemplate(double, T) MyFunction, double t, double v)
    {
        double sum = 0;
        double weight = 0;
        double buf =0;

        for( int i = 0, n = data.size(); i < n; i++)
        {
            buf = data[i].pv(t, v);

            sum = sum + buf;

            weight = weight + MyFunction(&data[i], t, v) * buf;
        }

        return weight/sum;
    }

};

int main()
{
    Master<zc> A;

    for (int i = 1; i < 10; i++)
        A.data.push_back(zc(i, 0.1, 100));

    A.data.push_back(zc(10, 0.1, 1100));


    cout << A.pv(0, 0) << endl;
    cout << A.d(0, 0) << endl;
    cout << A.c(0, 0) << endl;

    return 0;
}

答案 2 :(得分:0)

执行这些操作的常见C ++习惯用法是传递函数“object”并将其作为模板参数接受:

#include <algorithm>

// definition of class A by user http://stackoverflow.com/users/320726/6502
// in answer http://stackoverflow.com/a/18944672/420683
struct A
{
    double x, y;
    A(double x, double y) : x(x), y(y) {}
    double sum() const { return x + y; }
    double max() const { return std::max(x, y); }
};

template <typename T>
struct B
{
    std::vector<T> data;
    template<class Function>
    double average(Function f)
    {
        double sum = 0;
        int n = data.size();
        for (int i = 0; i < n; i++)
            sum = sum + f(data[i]);
        return sum / n;
    }
};

#include <functional>

int main()
{
    B<A> b{ {{1.0, 42.0}, {2.0, 43.0}, {3.0, 44.0}} };
    b.average([](A const& p){ return p.sum(); });
    b.average(std::mem_fn(&A::sum));
}

这是一种非常通用的方法,因为它不仅接受函数指针或成员函数指针,还接受任何类型的可调用对象。通过std::mem_fn使用成员函数指针很简单。