2

Greating everybody! I have a function-pointer method

int Myclass::*myMethod(char* a,char* b){
  //some code
}

And try to run it

bool Myclass::myMethod2(){
    AnotherClass *instance = AnotherClass:getInstance();
    instance-> addParams(&myMethod);
    return true;
}

AnotherClass - this class in another dll. AnotherClass definition

class AnotherClass
{
    //friend class Myclass;
public:
    static AnotherClass* getInstance();
    void addParams(int (*myMethod)(char*, char*) =0);
       //I try so void addParams(int (Myclass::*myMethod)(char*, char*) =0);
};

And have error C2664. Cannot convert parameter 1 from 'int Myclass::* (__cdecl *)(char *,char *)' to 'int (__cdecl *)(char *,char *).

Hm.. What should i do?

Curt
  • 94,964
  • 60
  • 257
  • 340
Stepchik
  • 267
  • 1
  • 2
  • 14
  • 4
    It's impossible to convert a member function pointer to a non-member function pointer. What is it that you're really trying to do here? There might be a better way to do what you want. – In silico Jul 18 '11 at 10:02
  • `AnotherClass` looks badly-designed, since it takes just a function pointer, with no context belonging to the caller. Your caller apparently wants to provide some context (when calling a non-static member function `MyMethod` on a particular instance of `Myclass`, you need something for `this` to point to). C-style callback interfaces usually have a user data pointer for this, in C++ you can do the same, or use polymorphism. Depending how important the dll boundary is, you could perhaps make `addParams` a template. – Steve Jessop Jul 18 '11 at 10:20
  • possible duplicate of [Disabling "bad function cast" warning](http://stackoverflow.com/questions/6676764/disabling-bad-function-cast-warning) – iammilind Jul 18 '11 at 10:28

3 Answers3

2

You can't.

The addParams() method needs a function that accepts two char* arguments.

Myclass::myMethod accepts two char* arguments and a Myclass object.

There's no way to make the two compatible.

n. 'pronouns' m.
  • 95,181
  • 13
  • 111
  • 206
2

EDIT: I misread your question slightly, and didn't notice the bit about the DLL - but one and three still applies if you have control over the DLL, and have the desire to modify it. If not, all you can do is number two.


In this case there are three things you could do:

One, change the way your AnotherClass is designed by declaring addParams like so:

void addParams(int (Myclass::*)(char*, char*) =0);

Two, you could make Myclass::myMethod() a static member, and then your original declaration of addParams would work.

Three, you could use a library like boost to bind the method as follows:

bool Myclass::myMethod2(){
    AnotherClass *instance = AnotherClass:getInstance();
    instance-> addParams(boost::bind(&Myclass::myMethod, this));
    return true;
}

class AnotherClass
{
    //...
    void addParams(boost::function<int(char*, char*)>);
};
Nate
  • 11,467
  • 5
  • 42
  • 59
0

You're invoking the method from inside an instance of an object. (Assuming Myclass::myMethod2() is not static)

You can call the method from this:

instance-> addParams(&(this->myMethod));
Yochai Timmer
  • 44,746
  • 21
  • 135
  • 179