0

I have the following problem. I have a base abstract class with a pure virtual method, and I want to pass it as an argument to another member function(so not a normal function). Yet I have an error when trying to call the method with specified function. Code speaks better than words so bellow I have posted the code that generates the problem

class BaseClass
{
public:
    BaseClass();
    int add(int, int);
    virtual void op(void (*f)(int, int), string s, int a, int b) = 0;
    ~BaseClass();
};
#include "BaseClass.h"
class ClasaDerviata:public BaseClass
{
public:

    ClasaDerviata();
    void testNumere(int a, int b);
    void op(void(*f)(int, int), string s, int a, int b);
    ~ClasaDerviata();
};
#include "BaseClass.h"

BaseClass::BaseClass()
{
}
int BaseClass::add(int a, int b)
{
    return a + b;
}

BaseClass::~BaseClass()
{
}

#include "ClasaDerviata.h"
#include <iostream>

using namespace std;


ClasaDerviata::ClasaDerviata()
{
}
void ClasaDerviata::testNumere(int a, int b)
{
    cout << a + b << "\n";
    cout << " suma " << add(a,b) << "\n";
}
void ClasaDerviata :: op (void (*f)(int, int), string s, int a, int b)
{
    f(a, b);
   cout << s << "\n";
}
ClasaDerviata::~ClasaDerviata()
{
}
#include "ClasaDerviata.h"
#include <iostream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    ClasaDerviata *a;
    a = new ClasaDerviata();
    int x, y;
    cin >> x >> y;
    a->op(&ClasaDerviata::testNumere, "test metoda", x, y);

    system("pause");

    return 0;
}

Thank you for your time!

Mircea Paul Muresan
  • 628
  • 1
  • 9
  • 23
  • 1
    "an error" what error? – Daniel Daranas Jul 07 '15 at 11:55
  • IntelliSense: argument of type "void (ClasaDerviata::*)(int a, int b)" is incompatible with parameter of type "void (*)(int, int)" and Error 1 error C2664: 'void ClasaDerviata::op(void (__thiscall BaseClass::* )(int,int),std::string,int,int)' : cannot convert argument 1 from 'void (__thiscall ClasaDerviata::* )(int,int)' to 'void (__thiscall BaseClass::* )(int,int)' – Mircea Paul Muresan Jul 07 '15 at 11:56
  • You should add that to the question. Also, the error message explains the problem to you. – Daniel Daranas Jul 07 '15 at 11:57
  • 1
    possible duplicate of [Calling C++ class methods via a function pointer](http://stackoverflow.com/questions/1485983/calling-c-class-methods-via-a-function-pointer) – NathanOliver Jul 07 '15 at 11:59

2 Answers2

2

void ClasaDerviata::testNumere(int a, int b); is not of type void (*)(int, int) but void (ClasaDerviata::*)(int, int)

You may add static to testNumere and add to fix your problem or change signature of your function (and change internal code too).

Jarod42
  • 203,559
  • 14
  • 181
  • 302
  • @MMp131316 Note that if you make your class member function static then you will no longer be able to use any non static members of the class in the function. I know you are not using any in your example/code but I just figured I would point it out. – NathanOliver Jul 07 '15 at 12:03
  • well I understand that, however for now this seems like a good work arround. Maybe there is a better solution..? – Mircea Paul Muresan Jul 07 '15 at 12:05
  • You could partially apply the member function when you pass it, so the object pointer doesn't need to be specified in the type. That way you can pass both member and non-member functions. `std::function` can help with this. – OMGtechy Jul 07 '15 at 12:57
  • I realized that applying the member function (not making it static) is more of what I want, that is why the idea wih a static is ok as work around but not what I am looking for – Mircea Paul Muresan Jul 07 '15 at 17:01
1

Remember when you make a call to a member function a hidden parameter 'this' is passed. So your ClasaDerviata::testNumere(int a, int b); function actually takes three parameters. I would suggest to read Joseph Garvin comment in How can I pass a class member function as a callback?

he has explained it very well.

Community
  • 1
  • 1
Vikram
  • 76
  • 5