如何持有成员函数指针及装饰后成员函数指针实现动态过滤逻辑?
Alright, let's tackle this problem step by step. You need a complexFilteringProcedure that lets users pick filtering rules for Thing objects, using both raw member function pointers and decorated versions of those methods. Here's a practical, maintainable implementation:
Thing Class with Private Filter Methods First, we'll set up the Thing class with its private filtering logic. Since we need external access to these private methods, we'll declare our procedure function as a friend to avoid exposing the filters directly:
#include <iostream> #include <functional> #include <string> class Thing { private: void filterStrong() { std::cout << "Running strong filter: removing high-intensity items\n"; // Add actual strong filter logic here } void filterWeak() { std::cout << "Running weak filter: removing only critical items\n"; // Add actual weak filter logic here } // Grant access to our complex procedure friend void complexFilteringProcedure(Thing& thing, const std::string& filterChoice); };
Next, we'll define a type for our member function pointers (to clean up the code) and build a decorator to add extra behavior to the raw filter methods (like logging, pre/post processing):
// Alias for Thing's filter member function pointer type using ThingFilterPtr = void (Thing::*)(); // Decorator that wraps a raw filter method with extra workflow logic std::function<void(Thing&)> decorateFilter(ThingFilterPtr rawFilter) { return [rawFilter](Thing& thing) { std::cout << "--- Pre-filter setup complete ---\n"; (thing.*rawFilter)(); // Execute the raw private filter std::cout << "--- Post-filter validation passed ---\n"; }; }
complexFilteringProcedure Now we'll build the core function. It will:
- Select the correct filter based on user input
- Hold both the raw member function pointer and its decorated version
- Execute the full complex filtering workflow
void complexFilteringProcedure(Thing& thing, const std::string& filterChoice) { // Hold the raw member function pointer ThingFilterPtr selectedRawFilter = nullptr; // Map user choice to the corresponding private filter if (filterChoice == "strong") { selectedRawFilter = &Thing::filterStrong; } else if (filterChoice == "weak") { selectedRawFilter = &Thing::filterWeak; } else { std::cerr << "Error: Invalid filter choice!\n"; return; } // Hold the decorated filter (with extra workflow steps) auto decoratedFilter = decorateFilter(selectedRawFilter); // Run the full complex procedure std::cout << "=== Initiating complex filtering procedure ===\n"; decoratedFilter(thing); // Add any additional shared procedure logic here (e.g., final reporting) std::cout << "=== Complex filtering procedure completed ===\n\n"; }
You can test this setup with a simple main function to verify it works as expected:
int main() { Thing myProjectThing; // Test strong filter selection complexFilteringProcedure(myProjectThing, "strong"); // Test weak filter selection complexFilteringProcedure(myProjectThing, "weak"); // Test invalid choice complexFilteringProcedure(myProjectThing, "medium"); return 0; }
Key Details to Note
- Private Method Access: Using a friend function is a clean way to access
Thing's private filters without exposing them publicly. If you prefer not to use friends, you could add public getter methods inThingthat return the member function pointers. - Member Function Pointers: The
ThingFilterPtrtype lets us dynamically select and store references toThing's filter methods at runtime, which is critical for supporting user-selected rules. - Decorator Pattern: The
decorateFilterfunction adds reusable logic (like setup/validation) to the raw filters, keeping the core filter code focused and the procedure modular.
内容的提问来源于stack exchange,提问作者penelope

