0

There are four classes.
Base, Derived1, Derived2, BaseUser.
Derived1 and Derived2 inherits Base class.
The final purpose is using functions of Derived1 and Derived2 on BaseUser codes through virtual function in Base class. (I'm implementing api server, and i want to set different api functions depending on certain condition.)

//base_user.h
BaseUser::SetBase(Base* base);
    
//other cc file
BaseUser user;
Derived1 dev1;
Derived2 dev2;
condition ? user.SetBase(&dev1) : user.SetBase(&dev2);

By the way, There is an typedef-ed funciton pointer on base.cc source code.
(It doesn't matter for me whether the type definition is in the class or outside the class.)
I'll call the type as func_ptr from now on.

public: 
    typedef int(*func_ptr)(int v1, int v2, int v3);

Also Base class has a vector of the type as a member.

protected:
    std::vector<std::pair<int, func_ptr>> functions;

I want to use the functions in Deriveds by inserting the functions of them into vector functions.

if I define

int func1(int v1, int v2, int v3);

in any Derived class, and try

functions.push_back(std::pair<int, func_ptr>(1, &Derived::func1));

it shows the message that says the argument types are different each othe. such like below

no instance of constructor "int, func_ptr" matches -- argument type int (Derived::*)(int v1, int v2, int v3)

I think it is not a problem only on std::pair. It seems like the types of func_ptr and func1 are different each other because the function func1 is defined in the Derived class.

How can I implement it?

virtual code (not tested)

//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));
  //...//
}
Jaehyung Kim
  • 283
  • 3
  • 13
  • 1
    please include a [mcve] and the compiler error message – 463035818_is_not_an_ai Oct 20 '20 at 07:51
  • 1
    `&Derived1::func1` is not a `int(*func_ptr)(int v1, int v2, int v3);` (the error message should have told you that). Pointers to member functions are not pointers to free functions – 463035818_is_not_an_ai Oct 20 '20 at 07:53
  • 1
    Pointers to non-static member functions are *not* the same a pointers to non-member functions. You can't convert between them in any way. I recommend you learn about [`std::function`](https://en.cppreference.com/w/cpp/utility/functional/function) and [lambda expressions](https://en.cppreference.com/w/cpp/language/lambda). – Some programmer dude Oct 20 '20 at 07:53
  • 1
    related: https://stackoverflow.com/questions/12662891/how-can-i-pass-a-member-function-where-a-free-function-is-expected (didn't find a good dupe) – 463035818_is_not_an_ai Oct 20 '20 at 07:55
  • thanks for your answers! sorry for no reproducible example. I'm not used to c++, so it takes too long time to write the code. you mean there is no way to do this. okay, thank you! I think I have to study more about c++ functions. – Jaehyung Kim Oct 20 '20 at 08:34
  • 1
    "you mean there is no way to do this" no. There is a way to do this. The answers in the Q&A i linked above explain the difference between pointer to method and pointer to free function – 463035818_is_not_an_ai Oct 20 '20 at 09:17
  • @idclev463035818 sorry, I didn't actually check the linked Q&A. you said you didn't find a good dupe, however i think, as I checked, the Q&A is exactly what I have looked for. (It is difficult for me to write correct C++ questions in correct sentences, because I'm not good at both of English and C++..) After I resolve my problem, I'm going to write reproducible example code for incorrect usage and correct usage as an answer of my question. Thank you for your help! – Jaehyung Kim Oct 21 '20 at 02:39

0 Answers0