在类中作为参数传递函数 我有问题在C++中传递一个函数作为参数。我在这里重新创建了一个最小的工作示例: #include <iostream> using namespace std; void print(const double &func(double)) { cout << func(1) << endl; } class Obj { public: double MyFunction(double x); void PrintFunction(); }; void Obj::PrintFunction(){ print(MyFunction); } double Obj::MyFunction(double x) { return x + 1; } int main() { Obj object; object.PrintFunction(); } #包括 使用名称空间std; 无效打印(常量双精度和函数(双精度)){ cout

在类中作为参数传递函数 我有问题在C++中传递一个函数作为参数。我在这里重新创建了一个最小的工作示例: #include <iostream> using namespace std; void print(const double &func(double)) { cout << func(1) << endl; } class Obj { public: double MyFunction(double x); void PrintFunction(); }; void Obj::PrintFunction(){ print(MyFunction); } double Obj::MyFunction(double x) { return x + 1; } int main() { Obj object; object.PrintFunction(); } #包括 使用名称空间std; 无效打印(常量双精度和函数(双精度)){ cout,c++,function,C++,Function,您可以使用std::bind传递成员方法 #include <iostream> #include <memory> #include <functional> void print(std::function<double(double)> f) { std::cout << f(1) << std::endl; } class Obj { public: double MyFunction(doubl

您可以使用std::bind传递成员方法

#include <iostream>
#include <memory>
#include <functional>

void print(std::function<double(double)> f) {
    std::cout << f(1) << std::endl;
}

class Obj {
  public:
  double MyFunction(double x);
  void PrintFunction();
};

void Obj::PrintFunction(){
    auto t = std::bind(&Obj::MyFunction,this,std::placeholders::_1);
    print(t);
}

double Obj::MyFunction(double x) {
  return x + 1;
}

int main() {
  Obj object;
  object.PrintFunction();
}

#包括
#包括
#包括
无效打印(标准::函数f){

std::cout您可以使用std::bind传递成员方法

#include <iostream>
#include <memory>
#include <functional>

void print(std::function<double(double)> f) {
    std::cout << f(1) << std::endl;
}

class Obj {
  public:
  double MyFunction(double x);
  void PrintFunction();
};

void Obj::PrintFunction(){
    auto t = std::bind(&Obj::MyFunction,this,std::placeholders::_1);
    print(t);
}

double Obj::MyFunction(double x) {
  return x + 1;
}

int main() {
  Obj object;
  object.PrintFunction();
}

#包括
#包括
#包括
无效打印(标准::函数f){

std::我能用两种方法编译一个完整的最小示例吗,请检查。受此启发。我用两种方法编译了一个完整的最小示例,请检查。受此启发。