How to pass a function by reference?

I have a C ++ program that has free functions.

Since most of the team has little experience with object-oriented design and programming, I need to refrain from objects of functions.

I want to pass a function to another function, for example for_each. I usually use a function pointer as a parameter:

typedef void (*P_String_Processor)(const std::string& text);
void For_Each_String_In_Table(P_String_Processor p_string_function)
{
  for (unsigned int i = 0; i < table_size; ++i)
  {
    p_string_function(table[i].text);
  }
}

I want to remove pointers, as they can point anywhere and contain invalid content.

Is there a way to pass a function by reference, similar to passing by pointer, without using a function object?

Example:

  // Declare a reference to a function taking a string as an argument.
  typedef void (??????);  

  void For_Each_String_In_Table(/* reference to function type */ string_function);
+4
source share
1 answer

(*&):

typedef void (&P_String_Processor)(const std::string& text);
+7

All Articles