我想重新分配一个具有自引用的类,名为CarJoker
“重新分配”在这里意味着=更改对象的地址。

此技术对于使CarJoker的每个实例都位于可调整大小的连续数组(例如,数组)中是必需的。池。

我考虑使用std::move,但是它不能以我希望的方式移动CarJoker::wheels
MCVE

#include <vector>
#include <iostream>
#include <string>
struct Wheel{
    void setBlade(){}
    void setTwinkle(){}
};
struct CarJoker{
    Wheel wa;
    Wheel wb;
    Wheel wc;
    std::vector<Wheel*> wheels;
    float hp=5;
    CarJoker(){
        wheels.push_back(&wa);
        wheels.push_back(&wb);
        wheels.push_back(&wc);
    }
    void wow(){
        //v want to apply something to every "wheel"
        for(auto wheel:wheels){
            wheel->setBlade();
        }
        //v want to apply something to some certain "wheel"
        wa.setTwinkle();
    }
};

int main(){
    CarJoker car1;
    CarJoker car2=std::move(car1);
    std::cout<<"want to 1 : "<<(car2.wheels[0]== &car2.wa)<<std::endl;
}


使用std::move时,car2.wheels[0]指向&car1.wa而不是我希望的&car2.wa
我知道原因,但这不是我的目标,而且我不知道解决问题的优雅方法。

我可怜的解决方法

这是一种优雅的方式(MCVE):

struct CarJoker{
    Wheel wa;
    Wheel wb;
    Wheel wc;
    std::vector<Wheel*> wheels;
    float hp=5;
    CarJoker(){
        reIni();  //: CHANGE (call a new function)
    }
    void reIni(){ //: CHANGE (create a new function)
        wheels.clear();
        wheels.push_back(&wa);
        wheels.push_back(&wb);
        wheels.push_back(&wc);
    }
    void wow(){
        //v want to apply something to every "wheel"
        for(auto wheel:wheels){
            wheel->setBlade();
        }
        //v want to apply something to some certain "wheel"
        wa.setTwinkle();
    }
};
int main(){
    CarJoker car1;
    CarJoker car2=std::move(car1);
    car2.reIni(); //: CHANGE (call a new function)
    std::cout<<"want to 1 : "<<(car2.wheels[0]== &car2.wa)<<std::endl;
}


坏处:-
1.很脏。
2.我必须为池中存在此类症状的每个类创建一个特殊名称函数(reIni())。我的池也必须识别该功能(例如,使用模板或虚拟功能进行注册)。

我可怜的解决方法2

 struct CarJoker{
    Wheel wa;
    Wheel wb;
    Wheel wc;
    std::vector<Wheel*> getWheels(){  //use this instead of "wheels"
        std::vector<Wheel*> re;
        re.push_back(&wa);
        re.push_back(&wb);
        re.push_back(&wc);
        return re;
    }
    ....
 }


我会工作,但我觉得这样的解决方法很疯狂。
限制增加了编码人员的陷阱。

如果wheels碰巧需要缓存计算量大的结果,那么现在经常调用getWheels()会很昂贵。

最佳答案

您不需要reIni()方法。您需要添加:


复制构造函数和移动构造函数,它们都以与默认构造函数相同的方式初始化wheels成员。
不复制/移动wheels成员的副本分配运算符和移动分配运算符。


尝试这个:

struct CarJoker{
    Wheel wa;
    Wheel wb;
    Wheel wc;
    std::vector<Wheel*> wheels;
    float hp = 5;

    CarJoker(){
        wheels.push_back(&wa);
        wheels.push_back(&wb);
        wheels.push_back(&wc);
    }

    CarJoker(const CarJoker &src) :
        CarJoker(),
        wa(src.wa),
        wb(src.wb),
        wc(src.wc),
        //wheels(src.wheels),
        hp(src.hp){
    }

    CarJoker(CarJoker &&src) :
        CarJoker(),
        wa(std::move(src.wa)),
        wb(std::move(src.wb)),
        wc(std::move(src.wc)),
        //wheels(std::move(src.wheels)),
        hp(src.hp){
    }

    // copy assignment and move assignment can be
    // handled with a single implementation that
    // lets the compiler choose between the copy
    // constructor and move constructor as needed...
    CarJoker& operator=(CarJoker rhs){
        wa = std::move(rhs.wa);
        wb = std::move(rhs.wb);
        wc = std::move(rhs.wc);
        wheels = std::move(rhs.wheels);
        hp = rhs.hp;
        return *this;
    }

    ...
};


话虽如此,您真的不应该使用自引用字段作为开始。单个std::array<Wheel, 3>字段比3个Wheel字段和std::vector<Wheel*>字段有意义,避免了整个问题。

struct CarJoker{
    std::array<Wheel, 3> wheels;
    float hp = 5;

    CarJoker() = default;
    CarJoker(const CarJoker&) = default;
    CarJoker(CarJoker&&) = default;
    CarJoker& operator=(const CarJoker&) = default;
    CarJoker& operator=(CarJoker&&) = default;

    Wheel& wa() { return wheels[0]; }
    const Wheel& wa() const { return wheels[0]; }

    Wheel& wb() { return wheels[1]; }
    const Wheel& wb() const { return wheels[1]; }

    Wheel& wc() { return wheels[2]; }
    const Wheel& wc() const { return wheels[2]; }

    void wow(){
        //v want to apply something to every "wheel"
        for(auto &wheel : wheels){
            wheel.setBlade();
        }

        //v want to apply something to some certain "wheel"
        wa().setTwinkle();
    }
};

10-06 07:06