2

If I have:

class MyClass {
 ...... my code....
private:
 void myFunction(int p);

}

in cpp file:

MyClass::MyClass() {

void (*pointer)(int);
pointer = &myFunction;

}

MyClass::myFunction(int p) {
//.... my code .... //
}

Is possible this? I have an assignment problem:

cannot convert 'void(MyClass::*)(int)' to 'void (*)(int)' in assignment
Safari
  • 11,437
  • 24
  • 91
  • 191
  • The compiler gives you a pretty good hint: the type of a pointer to that member function is `void(MyClass::*)(int)`. – juanchopanza Jun 26 '13 at 14:38

3 Answers3

3

You need to typedef a pointer type like this:

typedef void (MyClass::*FnPointer)(int);

and create a pointer of that type like this:

FnPointer pnt = &MyClass::myFunction;

The function can be called like this:

MyClass obj;
(obj.*pnt)( 3 );

I don't know if OP has an aim in mind, or is just playing, but this approach is useful for creating a callback or delegate. A structure would hold and be initialised with a reference or pointer to an instance of a class, and a pointer to a member function of that class.

See: What is a C++ delegate?

Community
  • 1
  • 1
Grimm The Opiner
  • 1,778
  • 11
  • 29
  • +1 But how would the call work? When any other class variable is accessed? – sethi Jun 26 '13 at 14:50
  • ok, now I have to pass this pointer in the constructor of another object: how should I declare this constructor: MySecondClass (FnPointer myPointer)?? – Safari Jun 26 '13 at 14:51
  • Once you've declared the typedef, there is a `type` called `FnPointer` which represents a "pointer to a function that is a member of `MyClass`". Use it the way you would any other type. Or to put it another way: Yes. :-) – Grimm The Opiner Jun 26 '13 at 14:58
  • Are you sure that (obj.*pnt)( 3 ); is correct? I use (this->*pnt)(3); but I have a compile error: pointer to member type ‘void (MyDataTypes::MyClass::)(int)’ incompatible with object type ‘MySecondClass’ MySecondClass is the class where I use the pointer – Safari Jun 26 '13 at 21:47
  • You need to edit your question to fill in this extra detail, but, if you're gonna call a function pointer defined as a member of `MyClass`, you need to call it on an instance of `MyClass`. – Grimm The Opiner Jun 27 '13 at 07:58
0

Actually the error message is very helpful in telling you what is wrong. You declared function pointer, but you have to declare method pointer.

Try typedef void (*pointer)(int); in declaration of the class, and then refer to the type by MyClass::pointer.

Michał Fita
  • 1,183
  • 1
  • 7
  • 24
-2

Method pointers are pretty useless unless you exploit their implementation details and use reinterpret_cast. See http://www.codeproject.com/Articles/7150/Member-Function-Pointers-and-the-Fastest-Possible if you really want to do this.

Now if you are running c++11 you can do this using lambda functions. Instead of void (*pointer)(int); you would use either std::function<void(int)> pointer; or auto pointer = <the lambda>.

Now instead of &myFunction or &MyClass::myFunction you would use [&this](int a){return this->myFunction(a);};. You can then assign that to an std::function variable or a type inferred variable (with auto).

Demos
  • 56
  • 3
  • Please explain why you consider pointers to _member functions_ to be "pretty useless". – Captain Obvlious Jun 26 '13 at 17:22
  • See the codeproject link! Essentially they point to a method of one class and only that class, and you need an instance of that class to call them. Because the author looks like he wants to assign a member function to a regular old function pointer I wager he cares about the functions param types and return types and not so much about what class it belongs to. – Demos Jun 27 '13 at 15:31
  • I've read the article and a good portion of it is crap and misleading The author **incorrectly** quotes the standard and seems to have a lot of misconceptions about why things are the way they are. That or he's simply ignoring them in an effort to pump up the reasoning for his article. He even goes Rambo about the aspects of undefined behavior but relies on it in his code. If something like that was posted on SO it would be shredded. – Captain Obvlious Jun 27 '13 at 17:01
  • yes, that is a really unsafe way to do this stuff, but I wager that there are some similar methods used in some of the delegate libraries around. There is really no reason to do that stuff in modern c++. It /does/ do a good just explaining the reasons behind why member pointers are the way they are. – Demos Jun 27 '13 at 19:01
  • The article is full of misconceptions and factual errors. It does a _very_ poor job at explaining the details of pointers to member functions. – Captain Obvlious Jun 27 '13 at 19:25