1

Possible Duplicate:
Disabling “bad function cast” warning

I am attempting to wrap my brain around c++ function pointers. To keep my learning experience basic, I created a test function pointer example. Eventually, I would like to pass all-ready instantiated objects by reference so I can callback the object's method; however, for the sake of learning and understand, I would like to stick to the basics of c++ function pointers. I created a working example just using a .cpp file, but the part that I am not succeeding at is using function pointers in .cpp and .h. What am I not doing correctly to get my learning example to work successfully when using .cpp and .h files?

I created two files, exeCallback.h and exeCallback.cpp.

.h file

/*
File: exeCallback.h

Header file for exeCommand Library.
*/

#ifndef EXECALLBACK_H
#define EXECALLBACK_H

#include "mbed.h"

#include <map>

class exeCallback
{
public:
    exeCallback();

    void my_int_func(int x);

    void (*foo)(int);
private:
};

#endif

.cpp file:

/*
File: exeCallback.cpp

Execute functions in other Sensor libraries/classes

Constructor
*/

#include "mbed.h"
#include "ConfigFile.h"
#include "msExtensions.h"
#include "cfExtensions.h"
#include "exeCallback.h"

exeCallback::exeCallback()
{

    foo = &exeCallback::my_int_func;

    /* call my_int_func (note that you do not need to write (*foo)(2) ) */
    foo( 2 );

}

void exeCallback::my_int_func(int x)
{
    printf( "%d\n", x );
}
Community
  • 1
  • 1
dottedquad
  • 1,371
  • 6
  • 24
  • 55
  • http://stackoverflow.com/questions/130322/how-do-you-pass-a-member-function-pointer – SomeWittyUsername Nov 21 '12 at 06:36
  • 1
    If possible, check out [`std::function`](http://en.cppreference.com/w/cpp/utility/functional/function) and [`std::bind`](http://en.cppreference.com/w/cpp/utility/functional/bind). The `std::function` class allows you to create a generic function wrapper that can hold any type of function, and the `std::bind` function allows you to bind any kind of function to a function-type variable. – Some programmer dude Nov 21 '12 at 06:39
  • @JoachimPileborg and the variadic constructor of `std::function` lets you do the binding in some cases without explicitly using `std::bind`, which is nice. – juanchopanza Nov 21 '12 at 06:45

1 Answers1

2

The error is telling you that you are trying to assign a pointer to a member function to a pointer to a (non-member) function. See [here](The error is telling you that you are trying to assign a pointer to a member function to a pointer to a (non-member) function.) for more on the differences. It looks like you need to declare foo as

void (exeCallback::*foo)(int);

Or make your life easier by using std::function (or boost::function if you don't have C++11 support).

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • I will have a look at your suggestion as soon as I get a basic understanding and a working example. I believe I am almost there. The error that I am now receiving is: "expression must have (pointer-to-) function type" I then tried exeCallback->foo(2), but I now receive the following two errors: expected an identifier and expected a type specifier – dottedquad Nov 21 '12 at 07:43
  • 1
    @dottedquad maybe it would be easier if you showed how you want to use the code. But member functions take an implicit pointer to an object of the type of their class parameter as first argument. Thus you probably need ` foo( this, 2 );` inside your callback. – juanchopanza Nov 21 '12 at 08:24
  • @juanchopanzo Eventually I would like to execute a function from text received via serial communication. So, I would not know how to show code along the lines of "how I would like to use the code" because I am still trying to get a basic working example of function pointers. I apologize that I may not be of any help when trying to explain what I am trying to accomplish. – dottedquad Nov 21 '12 at 08:47
  • @juanchopanzo After reading further into you explaination of " But member functions take an implicit pointer to an object of the type of their class parameter as first argument. Thus you probably need ` foo( this, 2 );` inside your callback." I am required to `(this->*foo)(2);` The new syntax seems to successfully work. – dottedquad Nov 21 '12 at 08:54
  • @dottedquad Good that you figured it out. BTW The two syntaxes are equivalent. The member access `.` and `->` are syntactic sugar. – juanchopanza Nov 21 '12 at 09:36
  • @juanchopanzo I did try your suggestion of `foo(this,2);`, but I still received the "expression must have (pointer-to-) function type" error. I do understand the two syntaxes are equivalent because, If I understand this correctly, the principle is to point to an object of the type of class? – dottedquad Nov 21 '12 at 09:48