what does this statement do? void (*update)(Blackboard&);?

February 15, 2010 - 4:12 am

im not sure what exactly is "update" but im sure "Blackboard" is a class. does this statement ring any bell for you? for example in below:
class Xclass{
public:
std::string representation;
const ModuleState* moduleState;
void (*update)(Blackboard&)
void (*create)();
void (*free)();
}

class Xclass{
public:
std::string representation;
const ModuleState* moduleState;
void (*update)(Blackboard&); // missing ;
void (*create)();
void (*free)();
}; // ; was missing

your code would not compile like this:
you need to have a forward declaration for
ModuleState and Blackboard (for example) like this:

class ModuleState;
class Blackboard;

In order to instantiate your class you need to have ModuleState and Blackboard defined somewhere in your code.

update, create, and free are pointers to static functions. Please check further down
The following is an application, of static function pointers used in a class:

class CPtr{
public:
// this is a pointer to a static - non-member - function taking 2 ints as parameters
int (*fct)(int, int);
CPtr( int (*)(int, int) );
};

// this is the static function the CPtr member
// it is passed as a parameter during instantiation
// of the CPtr object.
int max(int iA, int iB)
{
return iA > iB ? iA : iB;
}

// this is the constructor, which takes
// one argument, which is a pointer
// to the function, which should be used
// by the object
CPtr::CPtr(int (*Max)(int iA, int iB))
{
fct = Max;
}

int main(int argc, char* argv[])
{
// This is object a, instantiated with the
// function max. The function prototype
// is matching the declared function
// pointer in CPtr
CPtr a(max);

const int iA = 3, iB = 4;
// this is how the member is invoked
cout << "The bigger number between " << iA << " and " << iB << " is: " << a.fct(iA, iB)<<endl<<endl;
}

2 Responses to “what does this statement do? void (*update)(Blackboard&);?”

  1. Cubbi Says:

    It doesn’t have to ring bells, it’s basic C/C++ syntax.
    Your class Xclass contains a public member variable called update, which is a pointer to a non-member function that returns void and takes one argument of type reference-to-Blackboard. You are missing a semicolon after it.
    References :

  2. MichaelInScarborough Says:

    class Xclass{
    public:
    std::string representation;
    const ModuleState* moduleState;
    void (*update)(Blackboard&); // missing ;
    void (*create)();
    void (*free)();
    }; // ; was missing

    your code would not compile like this:
    you need to have a forward declaration for
    ModuleState and Blackboard (for example) like this:

    class ModuleState;
    class Blackboard;

    In order to instantiate your class you need to have ModuleState and Blackboard defined somewhere in your code.

    update, create, and free are pointers to static functions. Please check further down
    The following is an application, of static function pointers used in a class:

    class CPtr{
    public:
    // this is a pointer to a static - non-member - function taking 2 ints as parameters
    int (*fct)(int, int);
    CPtr( int (*)(int, int) );
    };

    // this is the static function the CPtr member
    // it is passed as a parameter during instantiation
    // of the CPtr object.
    int max(int iA, int iB)
    {
    return iA > iB ? iA : iB;
    }

    // this is the constructor, which takes
    // one argument, which is a pointer
    // to the function, which should be used
    // by the object
    CPtr::CPtr(int (*Max)(int iA, int iB))
    {
    fct = Max;
    }

    int main(int argc, char* argv[])
    {
    // This is object a, instantiated with the
    // function max. The function prototype
    // is matching the declared function
    // pointer in CPtr
    CPtr a(max);

    const int iA = 3, iB = 4;
    // this is how the member is invoked
    cout << "The bigger number between " << iA << " and " << iB << " is: " << a.fct(iA, iB)<<endl<<endl;
    }
    References :

Leave a Reply