Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/158.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在C+中创建包含不同类型函数指针的容器+;?_C++_Containers_Function Pointers_Virtual Machine_Genetic Programming - Fatal编程技术网

C++ 如何在C+中创建包含不同类型函数指针的容器+;?

C++ 如何在C+中创建包含不同类型函数指针的容器+;?,c++,containers,function-pointers,virtual-machine,genetic-programming,C++,Containers,Function Pointers,Virtual Machine,Genetic Programming,我正在做一个线性遗传规划项目,通过自然进化机制培育和进化程序。他们的“DNA”基本上是一个容器(我成功地使用了数组和向量),其中包含指向一组可用函数的函数指针。 现在,对于简单的问题,比如数学问题,我可以使用一个类型定义的函数指针,它可以指向所有返回一个double的函数,所有函数都以两个double作为参数 不幸的是,这不是很实际。我需要能够有一个容器,它可以有不同种类的函数指针,比如一个指向一个不带参数的函数的函数指针,或者一个带一个参数的函数,或者一个返回一些东西的函数,等等(你明白了)

我正在做一个线性遗传规划项目,通过自然进化机制培育和进化程序。他们的“DNA”基本上是一个容器(我成功地使用了数组和向量),其中包含指向一组可用函数的函数指针。 现在,对于简单的问题,比如数学问题,我可以使用一个类型定义的函数指针,它可以指向所有返回一个double的函数,所有函数都以两个double作为参数

不幸的是,这不是很实际。我需要能够有一个容器,它可以有不同种类的函数指针,比如一个指向一个不带参数的函数的函数指针,或者一个带一个参数的函数,或者一个返回一些东西的函数,等等(你明白了)

有没有什么方法可以使用任何类型的容器来实现这一点? 我可以使用一个包含多态类的容器来实现这一点吗?多态类又有各种各样的函数指针?
我希望有人能指导我找到一个解决方案,因为到目前为止,重新设计我所做的一切都将是痛苦的。

您不能将多态函数放入类中,因为获取(或返回)不同内容的函数不能以相同的方式(使用相同的接口)使用,这是多态性所必需的

让一个类为您需要的任何可能的函数类型提供一个虚拟函数的想法是可行的,但是(不知道您的问题!)它的用法让我觉得很奇怪:派生类会覆盖哪些函数?你的功能不是不相关吗

如果你的函数是不相关的(如果你没有理由把它们分组为同一个类的成员,或者如果它们是静态函数,因为它们不需要成员变量),你应该选择其他的。。。如果随机选取函数,则可以有几个不同的容器,一个用于函数类型,然后随机选取一个容器,然后在其中选取一个函数


你能举例说明你的函数做什么吗?

你不能把多态函数放在一个类中,因为接受(或返回)不同事物的函数不能以相同的方式使用(使用相同的接口),这是多态性所要求的

让一个类为您需要的任何可能的函数类型提供一个虚拟函数的想法是可行的,但是(不知道您的问题!)它的用法让我觉得很奇怪:派生类会覆盖哪些函数?你的功能不是不相关吗

如果你的函数是不相关的(如果你没有理由把它们分组为同一个类的成员,或者如果它们是静态函数,因为它们不需要成员变量),你应该选择其他的。。。如果随机选取函数,则可以有几个不同的容器,一个用于函数类型,然后随机选取一个容器,然后在其中选取一个函数


你能举例说明你的函数做什么吗?

虚拟机的一个典型想法是有一个单独的堆栈,用于参数和返回值的传递

您的函数仍然可以是void fn(void)类型,但您需要手动传递和返回参数

您可以这样做:

class ArgumentStack {
    public:
        void push(double ret_val) { m_stack.push_back(ret_val); }

        double pop() {
             double arg = m_stack.back();
             m_stack.pop_back();
             return arg;
        }

    private:
        std::vector<double> m_stack;
};
ArgumentStack stack;
// Multiplies two doubles on top of the stack.
void multiply() {
    // Read arguments.
    double a1 = stack.pop();
    double a2 = stack.pop();

    // Multiply!
    double result = a1 * a2;

    // Return the result by putting it on the stack.
    stack.push(result);
}
这可以通过以下方式使用:

// Calculate 4 * 2.
stack.push(4);
stack.push(2);
multiply();
printf("2 * 4 = %f\n", stack.pop());

您遵守了吗?

虚拟机的一个典型想法是使用一个单独的堆栈来传递参数和返回值

您的函数仍然可以是void fn(void)类型,但您需要手动传递和返回参数

您可以这样做:

class ArgumentStack {
    public:
        void push(double ret_val) { m_stack.push_back(ret_val); }

        double pop() {
             double arg = m_stack.back();
             m_stack.pop_back();
             return arg;
        }

    private:
        std::vector<double> m_stack;
};
ArgumentStack stack;
// Multiplies two doubles on top of the stack.
void multiply() {
    // Read arguments.
    double a1 = stack.pop();
    double a2 = stack.pop();

    // Multiply!
    double result = a1 * a2;

    // Return the result by putting it on the stack.
    stack.push(result);
}
这可以通过以下方式使用:

// Calculate 4 * 2.
stack.push(4);
stack.push(2);
multiply();
printf("2 * 4 = %f\n", stack.pop());

你明白吗?

你提到的内容本身可能可以通过一个
std::function
或有区别的联合体,如
Boost::variant

例如:

#include <functional>
#include <cstdio>
#include <iostream>

struct F {
  virtual ~F() {}
};

template< class Return, class Param = void >
struct Func : F {
  std::function< Return( Param ) >  f;
  Func( std::function< Return( Param ) > const& f ) : f( f ) {}
  Return operator()( Param const& x ) const { return f( x ); }
};

template< class Return >
struct Func< Return, void > : F {
  std::function< Return() >  f;
  Func( std::function< Return() > const& f ) : f( f ) {}
  Return operator()() const { return f(); }
};

static void f_void_void( void ) { puts("void"); }
static int f_int_int( int x ) { return x; }

int main()
{
  F  *f[] = {
    new Func< void >( f_void_void ),
    new Func< int, int >( f_int_int ),
  };

  for ( F **a = f, **e = f + 2;  a != e;  ++ a ) {
    if      ( auto p = dynamic_cast< Func< void >*     >( *a ) ) {
      (*p)();
    }
    else if ( auto p = dynamic_cast< Func< int, int >* >( *a ) ) {
      std::cout<< (*p)( 1 ) <<'\n';
    }
  }
}
#包括
#包括
#包括
结构F{
虚拟~F(){}
};
模板
结构函数:F{
std::函数<返回(参数)>f;
Func(std::functionconst&f:f(f){}
返回运算符()(Param const&x)const{Return f(x);}
};
模板<类返回>
结构函数:F{
std::functionf;
Func(std::functionconst&f):f(f){
返回运算符()()常量{Return f();}
};
静态无效f_void_void(void){puts(“void”);}
静态int f_int_int(int x){return x;}
int main()
{
F*F[]={
新函数(f_void_void),
新函数(f_int_int),
};
对于(F**a=F,**e=F+2;a!=e;++a){
如果(自动p=dynamic_cast*>(*a)){
(*p)();
}
else if(自动p=dynamic_cast*>(*a)){

std::cout您提到的内容本身可能可以通过
std::function
或有区别的联合体,如
Boost::variant

例如:

#include <functional>
#include <cstdio>
#include <iostream>

struct F {
  virtual ~F() {}
};

template< class Return, class Param = void >
struct Func : F {
  std::function< Return( Param ) >  f;
  Func( std::function< Return( Param ) > const& f ) : f( f ) {}
  Return operator()( Param const& x ) const { return f( x ); }
};

template< class Return >
struct Func< Return, void > : F {
  std::function< Return() >  f;
  Func( std::function< Return() > const& f ) : f( f ) {}
  Return operator()() const { return f(); }
};

static void f_void_void( void ) { puts("void"); }
static int f_int_int( int x ) { return x; }

int main()
{
  F  *f[] = {
    new Func< void >( f_void_void ),
    new Func< int, int >( f_int_int ),
  };

  for ( F **a = f, **e = f + 2;  a != e;  ++ a ) {
    if      ( auto p = dynamic_cast< Func< void >*     >( *a ) ) {
      (*p)();
    }
    else if ( auto p = dynamic_cast< Func< int, int >* >( *a ) ) {
      std::cout<< (*p)( 1 ) <<'\n';
    }
  }
}
#包括
#包括
#包括
结构F{
虚拟~F(){}
};
模板
结构函数:F{
std::函数<返回(参数)>f;
Func(std::functionconst&f:f(f){}
返回运算符()(Param const&x)const{Return f(x);}
};
模板<类返回>
结构函数:F{
std::functionf;
Func(std::functionconst&f):f(f){
返回运算符()()常量{Return f();}
};
静态无效f_void_void(void){puts(“void”);}
静态int f_int_int(int x){return x;}
int main()
{
F*F[]={
新函数(f_void_void),
新函数(f_int_int),
};
对于(F**a=F,**e=F+2;a!=e;++a){
如果(自动p=dynamic_cast*>(*a)){
(*p)();
}
else if(自动p=dynamic_cast*>(*a)){
我可以告诉你吗