You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何持有成员函数指针及装饰后成员函数指针实现动态过滤逻辑?

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:

Step 1: Define the 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);
};
Step 2: Create Member Function Pointer Type & Decorator

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";
    };
}
Step 3: Implement complexFilteringProcedure

Now we'll build the core function. It will:

  1. Select the correct filter based on user input
  2. Hold both the raw member function pointer and its decorated version
  3. 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";
}
Step 4: Test the Implementation

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 in Thing that return the member function pointers.
  • Member Function Pointers: The ThingFilterPtr type lets us dynamically select and store references to Thing's filter methods at runtime, which is critical for supporting user-selected rules.
  • Decorator Pattern: The decorateFilter function adds reusable logic (like setup/validation) to the raw filters, keeping the core filter code focused and the procedure modular.

内容的提问来源于stack exchange,提问作者penelope

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 08:09:48