0

我想重新分配一个具有自引用的类,名为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()经常调用会很昂贵。

4

1 回答 1

5

你不需要这个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;
    }

    ...
}; 

话虽这么说,你真的不应该一开始就使用自引用字段。单个字段比 3 个字段和一个字段std::array<Wheel, 3>更有意义,避免了整个问题。Wheelstd::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();
    }
};
于 2019-10-09T04:02:12.543 回答