c++ - 更改保持原始指针指向其字段的对象的地址
问题描述
我想重新分配一个具有自引用的类,名为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;
}
...
};
话虽这么说,你真的不应该一开始就使用自引用字段。单个字段比 3 个字段和一个字段std::array<Wheel, 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();
}
};
推荐阅读
- sparql - Prefered rank in Wikidata not working properly for population in some cases?
- ios - 二元运算符“==”不能应用于“字符串”和“UInt32”类型的操作数
- javascript - 期望返回值等于 $promise
- phaser-framework - Phaser Game Keyboard Arrow Keys to Mobile Touch
- django - Django: dynamic changing email recepients
- jquery - Generate Dynamic Inputs with Unique Names & IDs
- javascript - Can't manage to download PDF retrieved from BLOB via Servlet
- dialogflow-es - Dialogflow v2 用户输入和执行
- angularjs - How to change other element classes by clicking on an element inside ng-repeat
- python - (Py)Qt5:如何在以编程方式设置当前项目时更新选择