类内部 lambda 和 boost 函数

发布于 2024-11-07 14:39:24 字数 2407 浏览 0 评论 0原文

我使用 C++0x 的 lambda 来定义 boost 函数。我有一些类,我使用 lambdas/boost 函数定义它们的一些行为。我想知道的是,是否可以在不更改函数定义的情况下将有关类的数据公开到 lambda 上下文中?例如,我有一组这样的类:

class ConsumableItem : public Item
{
public:
    //Constructors here
    virtual bool Use(std::set<Character::BaseCharacter*> Users, std::set<Character::BaseCharacter*> Targets);
    virtual bool Use(std::set<Battles::BattleCharacter> Users, std::set<Battles::BattleCharacter> Targets, Battles::BattleField &field);
protected:
    bool UseRegularFunction;
    boost::function<void(std::set<Character::BaseCharacter*>, std::set<Character::BaseCharacter*>)> RegularUse;
    bool UseBattleFunction;
    boost::function<void(std::set<Battles::BattleCharacter>, std::set<Battles::BattleCharacter>, Battles::BattleField &)> BattleUse;
};

class StatBoostingItem : public ConsumableItem
{
public:
    StatBoostingItem(int UID, std::string &name, std::string &descript, boost::unordered_set<ItemFlags> Flags, boost::unordered_map<Stat, int> &boosts, int value);
protected:
    virtual bool Use(std::set<Character::BaseCharacter*> Users, std::set<Character::BaseCharacter*> Targets) override;
    virtual bool Use(std::set<Battles::BattleCharacter> Users, std::set<Battles::BattleCharacter> Targets, Battles::BattleField &field) override;
private:
    boost::unordered_map<Stat, int> StatBoosts;
}

在 stat boosting 项目的构造函数内,我希望能够执行此操作:

StatBoostingItem::StatBoostingItem(int UID, std::string &name, std::string &descript, boost::unordered_set<ItemFlags> Flags, boost::unordered_map<Stat, int> &boosts, int value)
    : ConsumableItem(UID, name, descript, Flags, ItemClass::STAT_BOOSTING_ITEM, value)
{
    StatBoosts = boosts;
    RegularUse = [](std::set<Character::BaseCharacter*> users, std::set<Character::BaseCharacter*> targets)
    {
        for (auto character = targets.begin(); character != targets.end(); ++character)
        {
            for (auto statBoost = StatBoosts.begin(); statBoost != StatBoosts.end(); ++statBoost)
            {
                //Item Effect in here
            }
        }
    };
}

这更令人好奇,因为我知道我可以简单地扩展 boost::function定义包含指向相关项目的指针并以这种方式获取数据,或者覆盖我的 Use 函数。但是,如果我能以某种方式将有关包含它的类的信息公开到 lambda 中,那将是理想的,因为我也可以在其他地方使用它

I'm using C++0x's lambdas to define boost functions. I have some classes where I define some of their behaviour using lambdas/boost functions. What I was wondering is, is it possible to expose data about the classes into the lambda's context without altering the function definition? For example, I have a set of class like this:

class ConsumableItem : public Item
{
public:
    //Constructors here
    virtual bool Use(std::set<Character::BaseCharacter*> Users, std::set<Character::BaseCharacter*> Targets);
    virtual bool Use(std::set<Battles::BattleCharacter> Users, std::set<Battles::BattleCharacter> Targets, Battles::BattleField &field);
protected:
    bool UseRegularFunction;
    boost::function<void(std::set<Character::BaseCharacter*>, std::set<Character::BaseCharacter*>)> RegularUse;
    bool UseBattleFunction;
    boost::function<void(std::set<Battles::BattleCharacter>, std::set<Battles::BattleCharacter>, Battles::BattleField &)> BattleUse;
};

class StatBoostingItem : public ConsumableItem
{
public:
    StatBoostingItem(int UID, std::string &name, std::string &descript, boost::unordered_set<ItemFlags> Flags, boost::unordered_map<Stat, int> &boosts, int value);
protected:
    virtual bool Use(std::set<Character::BaseCharacter*> Users, std::set<Character::BaseCharacter*> Targets) override;
    virtual bool Use(std::set<Battles::BattleCharacter> Users, std::set<Battles::BattleCharacter> Targets, Battles::BattleField &field) override;
private:
    boost::unordered_map<Stat, int> StatBoosts;
}

And inside the constructor for stat boosting item, I'd like to be able to do this:

StatBoostingItem::StatBoostingItem(int UID, std::string &name, std::string &descript, boost::unordered_set<ItemFlags> Flags, boost::unordered_map<Stat, int> &boosts, int value)
    : ConsumableItem(UID, name, descript, Flags, ItemClass::STAT_BOOSTING_ITEM, value)
{
    StatBoosts = boosts;
    RegularUse = [](std::set<Character::BaseCharacter*> users, std::set<Character::BaseCharacter*> targets)
    {
        for (auto character = targets.begin(); character != targets.end(); ++character)
        {
            for (auto statBoost = StatBoosts.begin(); statBoost != StatBoosts.end(); ++statBoost)
            {
                //Item Effect in here
            }
        }
    };
}

This is more a curiosity thing as I know I can simply extend the boost::function definition to include a pointer to the item in question and get the data that way, or override my Use functions. But if I could somehow expose information into lambda about the class that contains it, that would be ideal as I'd be able to make use of it elsewhere as well

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

后知后觉 2024-11-14 14:39:24

您是否需要从 lambda 访问 StatBoostingItem::StatBoostingItem 范围内可用的变量?在这种情况下,您可以简单地使用 捕获

RegularUse = [&](std::setusers, std::setTargets)

[&] 告诉 lambda 捕获来自参考当前范围。如果您只需要一个特定变量,则可以仅捕获它,也可以使用 [=] 按值捕获所有内容。我想您还可以使用 [this] 捕获指向 Item 实例的指针:

RegularUse = [this](std::setusers, std::set目标)

从来没有尝试过,但我认为它没有理由不起作用。如果它不起作用(因为 this 以某种方式保留,即使 lambda 不是方法),您始终可以通过将 this 分配给另一个变量并捕获来解决它相反:

Item* item = this;
RegularUse = [item](std::set<Character::BaseCharacter*> users, std::set<Character::BaseCharacter*> targets)
{
    item.DoSomething();
}

Do you need to access variables that are available in the scope of StatBoostingItem::StatBoostingItem from your lambda? In that case you could simply use captures:

RegularUse = [&](std::set<Character::BaseCharacter*> users, std::set<Character::BaseCharacter*> targets)

The [&] tells the lambda to captures everything from the current scope by reference. If you need just a specific variable, you can capture just it, and you could also use [=] to capture everything by value. I guess you can also use [this] to capture a pointer to the Item instance:

RegularUse = [this](std::set<Character::BaseCharacter*> users, std::set<Character::BaseCharacter*> targets)

Didn't ever try that, but I see no reason why it shouldn't work. If it doesn't work (because this is somehow reserved, even though lambdas aren't methods), you can always work around it by assigning this to another variable and capturing it instead:

Item* item = this;
RegularUse = [item](std::set<Character::BaseCharacter*> users, std::set<Character::BaseCharacter*> targets)
{
    item.DoSomething();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文