C++ 如何在子类中使用c+;中父类的类型定义函数指针类型的函数指针向量+;?

C++ 如何在子类中使用c+;中父类的类型定义函数指针类型的函数指针向量+;?,c++,function,pointers,inheritance,typedef,C++,Function,Pointers,Inheritance,Typedef,共有四节课。 Base、Derived1、Derived2、BaseUser. Derived1和Derived2继承基类。 最终目的是通过基类中的虚函数在BaseUser代码上使用Derived1和Derived2的函数。(我正在实现api服务器,我想根据特定条件设置不同的api函数。) 顺便说一下,base.cc源代码上有一个类型定义的函数指针。 (类型定义是在类中还是在类外对我来说无关紧要。) 从现在起,我将该类型称为func_ptr public: typedef int(*f

共有四节课。
Base、Derived1、Derived2、BaseUser.
Derived1和Derived2继承基类。
最终目的是通过基类中的虚函数在BaseUser代码上使用Derived1和Derived2的函数。(我正在实现api服务器,我想根据特定条件设置不同的api函数。)

顺便说一下,base.cc源代码上有一个类型定义的函数指针。
(类型定义是在类中还是在类外对我来说无关紧要。)
从现在起,我将该类型称为func_ptr

public: 
    typedef int(*func_ptr)(int v1, int v2, int v3);
基类还有一个类型为的向量作为成员

protected:
    std::vector<std::pair<int, func_ptr>> functions;
在任何派生类中, 试一试

我认为这不仅仅是std::pair的问题。func_ptr和func1的类型似乎彼此不同,因为函数func1是在派生类中定义的

我如何实现它

虚拟代码(未测试)

//Base.h
阶级基础{
公众:
typedef int(*func_ptr)(int v1、int v2、int v3);
//...//
受保护的:
向量函数;
虚拟void InitializeFunctionPointers();
}
//衍生的1.h
派生类1{
int func1(int v1、int v2、int v3);
int func2(int v1、int v2、int v3);
void InitializeFunctionPointers()重写;
//...//
公众:
Derived1();
}
//Derived1.cc
//...//
void Derived1::InitializeFunctionPointers(){
functions.push_back(std::pair(1,&Derived1::func1));
//...//
}

请包含一个,编译器错误消息
&Derived1::func1
不是
int(*func_ptr)(int v1、int v2、int v3)(错误消息应该告诉您)。指向成员函数的指针不是指向自由函数的指针指向非静态成员函数的指针与指向非成员函数的指针不同。你不能以任何方式在他们之间转换。我建议你了解和。相关:(没有找到一个好的傻瓜)“你的意思是没有办法做到这一点”不,有办法做到这一点。我上面链接的问答中的答案解释了指向方法的指针和指向自由函数的指针之间的区别
protected:
    std::vector<std::pair<int, func_ptr>> functions;
int func1(int v1, int v2, int v3);
functions.push_back(std::pair<int, func_ptr>(1, &Derived::func1));
no instance of constructor "int, func_ptr" matches -- argument type int (Derived::*)(int v1, int v2, int v3)
//Base.h
class Base{
  public:
  typedef int(*func_ptr)(int v1, int v2, int v3);
  //...//
  
  protected:
  std::vector<std::pair<int, func_ptr>> functions;
  virtual void InitializeFunctionPointers();
}

//Derived1.h
class Derived1{
  int func1(int v1, int v2, int v3);
  int func2(int v1, int v2, int v3);
  void InitializeFunctionPointers() override;
  //...//

  public:
  Derived1();
}

//Derived1.cc
//...//
void Derived1::InitializeFunctionPointers(){
  functions.push_back(std::pair<int, func_ptr>(1, &Derived1::func1));
  //...//
}