|18|error: invalid use of 'collection_of_funcs::ptr_to_chosen_func'|
|19|error: 'b' was not declared in this scope|
|19|error: 'choose_func' was not declared in this scope|
|25|error: 'ptr_to_chosen_func' does not name a type|
the issue seems to be with declaration of the typedefs and accessing the pointer within the class
The address of a member-function pointer must be obtained with the address-of operator. Type decay (in the form of the implicit function-to-pointer conversion) does not apply to non-static member functions: b = &collection_of_funcs::func_1; http://eel.is/c++draft/conv#func
- If your class type is really named "collection_of_functions", get rid of it and create a namespace. It is a design mistake to use a class merely to group names.
- Are you aware of std::function? Pointer-to-member functions have idiosyncrasies which can be eliminated from the user's perspective.
ptr_to_chosen_func is a member type of collection_of_funcs; choose_func is also a member function. collection_of_funcs::ptr_to_chosen_func b = ptr->choose_func(1);
- Do not dynamically-allocate memory just because you can. Dynamic allocation complicates the code, is slower, and (especially in the form of naked new and delete) has detrimental effects on exception safety.
1 2 3
collection_of_funcs fns;
auto pmf = fns.choose_func(1);
(fns.*pmf)();