自动将父指针传递给其变量

Automatically Pass Parent Pointer to its Variables?

本文关键字:变量 指针      更新时间:2023-10-16

这是我目前拥有的功能,我想知道是否有一种方法可以将父指针传递到它的所有变量,而不需要所有的= this

struct Location;
struct Parent
{
    std::vector<Location*> locations;
};
struct Location
{
    Location(Parent* p)
    {
        p->locations.push_back(this);
    }
};

struct SpecialParent : Parent
{
    Location a = this;
    Location b = this;
    Location c = this;
    Location zabaZoo = this;
    // etc
};

这样做的目的是,我仍然可以进行specialParent->zabaZoo,但仍然可以批量处理某些操作的位置。

Location a = b = c = zabaZoo = this;

敏捷的棕色狐狸跳过懒狗。

您可以创建一个SpecialParent构造函数,并使用赋值:

Location a, b, c, zabaZoo;
SpecialParent()
{
    a = b = c = zabaZoo = Location(this);
}