-1

trying to rewrite dome of the C code to C++, trying to abstract things so that we can have many different types of features tested in similar way. live on coliru

trying to form function pointers that have subclasses structures, so that i can abstract the mechanism for similar features. I cant give details on what, i have attached sample program here. any ideas appreciated. Is it possible to compile this code without the error

error: cannot convert 'void (Sample::)(Sample::sample_call_arg_s)' to 'Sample::sampleFuncType {aka void (Sample::)(call_arg_s)}'

#include <iostream>
#include <string>
#include <vector>
#include <map>
using namespace std;

struct call_arg_s
{
   string            time_stamp;         // when the call was made
   string            func_name;      // when the call was made
};

class Sample {
public:
      Sample();
      ~Sample();
    void initCallMap();
private:
   typedef void (Sample::*sampleFuncType)(call_arg_s*);
   struct sample_call_arg_s : call_arg_s
   {
      sampleFuncType func_pointer;
      sample_call_arg_s()
      {
         func_pointer = NULL;
      }
    };
    std::map<int, sample_call_arg_s> call_map;

    void sampleMethod( sample_call_arg_s* );
};

void Sample::initCallMap() 
{
   call_map[0]               = sample_call_arg_s();
   call_map[0].func_pointer  = &Sample::sampleMethod;
   call_map[0].func_name     = string("sampleMethod");
}

int main()
{
    Sample* sample = new Sample();
    sample->initCallMap();
    return 0;
}

Rephrased: I want to have a self contained class that will contain a list of class methods which will be run based on enums assigned to them in a thread. I want to abstract the idea so that, i can perform similar operation on different scenarios.

bicepjai
  • 1,615
  • 3
  • 17
  • 35
  • 3
    What is your question? – Algirdas Preidžius Oct 16 '15 at 00:19
  • I know it's not as easy when you're converting from C but generally there are better alternatives to function pointers in C++. I don't really understand what you're asking and without more context, (although I can understand if you can't give the details), it's difficult to see how this fits in but there are design patterns that you could use to help with this, and using templates or std::function will likely result in cleaner, more maintainable code. – stellarpower Oct 16 '15 at 00:25
  • Your typedef and your prototype are different, and parameters are not covariant (i.e. your function must accept any `call_arg_s`, not just an instance of a subtype). – molbdnilo Oct 16 '15 at 00:29

1 Answers1

0

The important point to note here is that a function with a signature void f1(Base*) is very different from a function with a signature void f2(Derived*) - they are not compatible. In the following:

Base b;
Derived d;
f2(&b); //illegal
f1(&d);

the illegal statement is kind of like what you're trying to do. A function pointer to a function accepting a Base* cannot be assigned to the address of a function accepting a Derived* because the function pointer might be passed a Base object that isn't a Derived. Equally, I don't think it works the other way around either, that is, assigning a function pointer accepting a Derived* to the address of a function accepting a Base* because the Derived pointer would possibly have to be adjusted first. The compiler inserts code to adjust the pointer to d, if necessary, BEFORE calling the function in the last line. f1 expects a Base* so it must have one. I'm afraid I'm too tired now to come up with a solution but I'll try and look in the morning, and as I said in the comment, there's probably a much better way of doing this.

stellarpower
  • 332
  • 3
  • 13
  • shant we use polymorphism since the sub class is (going to be, thats what i was planning to) a type of super class ? – bicepjai Oct 16 '15 at 00:47
  • So, if I understand correctly, you want a class that, if given another class, can associate that class' member functions with an enumerated value and so giving the enum, an object and a list of arguments this class will turn around and call the other class' function. Does this new class assign the enums or is this done externally? If you want the functions to be added at runtime you'll need to use an integer instead. Do all the functions take the same argument types? If not it will be possible but more complex and you'll need to decide on runtime or compile-time type checking. – stellarpower Oct 16 '15 at 12:20
  • i wa trying this way if handling functions pointers that can be executed from within an object but had issues with being able to combine them for creating a thread worker (using older c++ code base no thread lib). i found aother solution and figured what i was trying to do is impossible in c++. – bicepjai Oct 21 '15 at 03:33