How to typedef a function pointer

Question | Aug 2, 2019 | jmiller 

A function pointer is a pointer to a function, which can be invoked like the function itself. Function pointers are commonly used as callback arguments to other functions.

In the following code a class, Record, has a private vector named identifiers:

class Record {
public:
  // callback_t is typedef here
  void forEachId(callback_t callback) { 
    for(auto i : identifiers) 
      callback(i);
  }
  //.. more methods ..
private:
  std::vector<int> identifiers;
};

Record has the forEachId method that calls the passed callback function pointer argument for each int in vector identifiers. This is done to avoid exposing the private data member identifiers to Record's clients.

The forEachId method requires a function pointer argument of type callback_t. The callback_t is declared as a type of a function pointer to a function which:

  • Takes an int argument, and
  • Returns void

Select the correct typedef declaration of callback_t below:

2015 nextptr