I have a C++ program that has free standing functions.
Since most of the team has little experience with or knowledge of object oriented design and programming, I need to refrain from function objects.
I want to pass a function to another function, such as for_each
function. Normally, I would 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 since they can point to anywhere, and contain an invalid content.
Is there a method to pass a function by reference, similar to passing by pointer, without using 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);
只需将函数指针类型更改为函数引用( *
- > &
):
typedef void (&P_String_Processor)(const std::string& text);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.