C++运行时构建嵌套函数:基于动态输入实现Ability层级结构
基于运行时输入构建嵌套Ability结构的C++实现方案
你想要实现的是一个动态嵌套的能力系统——Ability由多个Action组成,每个Action包含多个Shape,每个Shape又绑定多种效果,而且要完全基于运行时输入来构建。结合C++的面向对象和动态特性,我给你梳理一套可落地的实现思路:
第一步:定义核心类层次
先把你的嵌套结构转化为C++类,用组合关系体现层级:
// 基础效果抽象类:所有具体效果都继承它 class Effect { public: virtual ~Effect() = default; // 每个效果的执行逻辑接口 virtual void execute() const = 0; }; // 具体效果示例:伤害效果 class DamageEffect : public Effect { private: int damageValue; public: DamageEffect(int d) : damageValue(d) {} void execute() const override { std::cout << "触发伤害效果:造成" << damageValue << "点伤害\n"; } }; // Shape类:绑定多个效果 class Shape { private: std::vector<std::unique_ptr<Effect>> effects; std::string shapeType; // 比如"圆形范围"、"矩形区域" public: Shape(std::string type) : shapeType(std::move(type)) {} // 给Shape添加效果 void addEffect(std::unique_ptr<Effect> effect) { effects.push_back(std::move(effect)); } // 触发Shape的所有效果 void trigger() const { std::cout << "\n触发Shape:" << shapeType << "\n"; for (const auto& eff : effects) { eff->execute(); } } }; // Action类:包含多个Shape class Action { private: std::vector<std::unique_ptr<Shape>> shapes; std::string actionType; // 比如"近战攻击"、"远程施法" public: Action(std::string type) : actionType(std::move(type)) {} void addShape(std::unique_ptr<Shape> shape) { shapes.push_back(std::move(shape)); } // 执行Action的所有Shape void perform() const { std::cout << "\n=== 执行动作:" << actionType << " ===\n"; for (const auto& shape : shapes) { shape->trigger(); } } }; // Ability类:包含多个Action class Ability { private: std::vector<std::unique_ptr<Action>> actions; std::string abilityName; public: Ability(std::string name) : abilityName(std::move(name)) {} void addAction(std::unique_ptr<Action> action) { actions.push_back(std::move(action)); } // 激活整个Ability void activate() const { std::cout << "\n===== 激活能力:" << abilityName << " =====\n"; for (const auto& action : actions) { action->perform(); } } };
第二步:实现运行时构建的工厂函数
为了根据用户输入动态组装整个结构,我们写一套工厂函数来解析输入并创建对象:
// 效果工厂:根据输入类型创建对应Effect std::unique_ptr<Effect> createEffect(const std::string& type, int param) { if (type == "damage") { return std::make_unique<DamageEffect>(param); } // 后续可以扩展更多效果类型,比如heal(治疗)、stun(眩晕)等 throw std::invalid_argument("未知效果类型:" + type); } // 从输入创建Shape std::unique_ptr<Shape> createShapeFromInput() { std::string shapeType; std::cout << "请输入Shape类型:"; std::cin >> shapeType; auto shape = std::make_unique<Shape>(shapeType); int effectNum; std::cout << "请输入该Shape的效果数量:"; std::cin >> effectNum; for (int i = 0; i < effectNum; ++i) { std::string effType; int param; std::cout << "请输入效果类型(如damage)和参数:"; std::cin >> effType >> param; shape->addEffect(createEffect(effType, param)); } return shape; } // 从输入创建Action std::unique_ptr<Action> createActionFromInput() { std::string actionType; std::cout << "\n请输入Action类型:"; std::cin >> actionType; auto action = std::make_unique<Action>(actionType); int shapeNum; std::cout << "请输入该Action的Shape数量:"; std::cin >> shapeNum; for (int i = 0; i < shapeNum; ++i) { action->addShape(createShapeFromInput()); } return action; } // 最终的Ability生成函数:完全基于运行时输入构建 std::unique_ptr<Ability> createAbilityFromRuntimeInput() { std::string abilityName; std::cout << "请输入Ability名称:"; std::cin >> abilityName; auto ability = std::make_unique<Ability>(abilityName); int actionNum; std::cout << "请输入该Ability的Action数量:"; std::cin >> actionNum; for (int i = 0; i < actionNum; ++i) { ability->addAction(createActionFromInput()); } return ability; }
第三步:使用示例
你可以这样调用生成函数,然后激活创建好的Ability:
int main() { auto myCustomAbility = createAbilityFromRuntimeInput(); myCustomAbility->activate(); return 0; }
扩展优化建议
- 如果需要更灵活的效果参数(比如多参数、不同类型),可以用
std::variant或者自定义参数结构体,替换示例里的int param。 - 要是想支持从配置文件(比如JSON)读取结构,可以引入nlohmann/json这类库,把控制台输入换成文件解析逻辑。
- 对于复杂的后续扩展(比如给整个结构加日志、序列化),可以引入访问者模式,避免频繁修改基类代码。
这样的实现既贴合你规划的嵌套结构,又能完全基于运行时输入动态构建,代码模块化程度高,后续扩展新的Action、Shape、Effect类型也很方便。
内容的提问来源于stack exchange,提问作者AuroraChroma
相关产品推荐
相关产品推荐

