2012-10-11 174 views
1

我正在写一个.hpp文件的类应该接收函数作为其参数之一,并将其​​存储在成员变量,到目前为止我的代码如下所示:C++类接受任何类型的函数作为参数

template<typename Function> class myClass 
{ 
    public: 
      myClass(Function function) : pvFunction(function) {} 

      //Functor operator 
      double operator() (double x) const{ 
      return pvFunction(x+4); 
      } 
    private: 
      Function pvFunction 
} 

该程序看起来毫无意义,因为它是,它返回的值现在不重要。我只是想弄清楚如何将一个函数传递给这个类并使用它的函数运算符。唯一的问题是:

1)我不知道这个类的定义是否正确,这是接受任何类型的函数作为参数传递给这个类的对象的正确方法。

2)如何在我的程序中创建此类的实例?如何将函数传递给新对象然后调用它?

去过这个相当长的一段时间,似乎无法弄清楚

编辑:

在我的程序文件,main.cpp中

,该代码会收到一个错误:

double function(double); 

int main() 
{ 
    myClass<double> myClassObject((function)); 
    return 0; 
} 

double function(double x) 
{ 
    return (x+3.0); 
} 
+0

查看['boost :: function <>'](http://www.boost.org/doc/libs/1_51_0/doc/html/function.html)的实现以获取灵感。 –

+0

你真的是指“任何类型”或“任何类型匹配某个参数类型列表和返回类型”吗? – juanchopanza

回答

1

1)是的。

2)的myClassObject从实施例中的类型应是

myClass<double(*)(double)> myClassObject(function); 

因为函数是double(*)(double)类型。构建这样的模板的对象,最好的办法是使用函数模板是这样的:

template<typename Function> 
myClass<Function> MakeMyClass(Function f) 
{ 
    return myClass<Function>(f); 
} 


int main() 
{ 
    auto withFuncPtr= MakeMyClass(function); 
    auto withLambda= MakeMyClass([] (double v) {return v*2;}); 

    Foo foo; 
    auto withStdFunction = MakeMyClass(
     std::bind(&Foo::Method, &foo, std::placeholders::_1)); 
    return 0; 
} 
+0

这正是我所需要的,非常感谢你 – imkendal

4

由于类预期模板参数为实际函数类型,但您在实例化中将模板参数作为double传递,所以出现错误。

考虑使用C++ 11的新功能,如std::function

template<typename T> 
struct myClass 
{ 
    typedef std::function<T(T)> func_type; 

    func_type pvFunction; 

    myClass(func_type func) : pvFunction(func) {} 

    T operator()(T x) 
     { return pvFunction(x + 4); } 
}; 

double function(double x) 
{ 
    return (x+3.0); 
} 

int main() 
{ 
    myClass<double> my_class(function); 
    std::cout << "my_class(3) = " << my_class(3) << '\n'; 
} 
+0

有趣,这肯定会更方便 – imkendal

0

您可以使用结构与运营商()的建议

template<typename Function> class myClass 
{ 
    public: 
     myClass(){} 

     //Functor operator 
     double operator() (double x) const 
     { 
      return pvFunction(x+4); 
     } 
    private: 
     Function pvFunction; 
}; 

struct Double 
{ 
    double operator()(const double n)const 
    { 
     return n; 
    } 
}; 

int main() { 
    myClass<Double> myClassObject; 
    std::cout<<myClassObject(2.0f); 
    return 0; 
} 

打印6

0

我真的不确定你想要实现什么,因为如果你仍然想要将它绑定到具有双参数类型的函数,你怎么能使用任何函数,但是,下面的例子使你的示例模板能够工作,但我真的不'没有看到使用。

template<typename Function> class myClass 
    { 
    public: 
     myClass(Function function) : pvFunction(function) {} 

     //Functor operator 
     double operator() (double x) const{ 
      return pvFunction(x+4); 
     } 
    private: 
     Function pvFunction; 
    }; 

    double foo1(double x) 
    { 
     return x; 
    } 

    double foo2(double x) 
    { 
     return x + 1; 
    } 
    int main(int argc, char** argv) 
    { 
     myClass<double(*)(double)> instance1(foo1); 
     myClass<double(*)(double)> instance2(foo2); 
     double a = instance1(1.5), b = instance2(1.5); 
     return 0; 
    } 
+0

最终这将成为一个派生类,但我需要能够存储函数来首先派生。我不希望人们为如何实现一个采用函数派生类的类提供建议,因此我将这些信息遗漏了。 – imkendal