[英]C++ How to properly copy container(vector) of pointers?
A few times I've stumbled across the scenario where I have a container of pointers that needs to be copied. 有几次我偶然发现了我有一个需要复制的指针容器的场景。
Let's say we have the following class hierarchy: 假设我们有以下类层次结构:
Student (base class) 学生(基础班)
StudentService StudentService
The StudentService class has a std::vector<Student*> students
field and the following constructor: StudentService类有一个
std::vector<Student*> students
字段和以下构造函数:
StudentService::StudentService(std::vector<Student*> students) {
// code
}
It won't be correct to just use the std::vector::operator=
operator and write this->students = students
, because that will only copy the pointer addresses and so if someone from the outside deletes the objects pointed to by those pointers, then the StudentService class would suffer as a result. 仅使用
std::vector::operator=
运算符并编写this->students = students
是不正确的,因为这只会复制指针地址,因此如果外部某人删除了那些指向的对象指针,然后StudentService类将受到影响。
The solution is to loop through each pointer in the students
parameter and create a new dynamic object, something like this: 解决方案是循环遍历
students
参数中的每个指针并创建一个新的动态对象,如下所示:
for(int i = 0; i < students.size(); i++) {
this->students.at(i) = new Student(*students.at(i));
}
But even that is not proper due to the fact that it will create ONLY Student objects. 但即使这样也不合适,因为它会创建ONLY Student对象。 And we know that a Student can be a Freshman, Sophmore, Junior or Senior.
我们知道学生可以是新生,索菲尔,初中或高级。 So here is my question: what's the best solution to this problem?
所以这是我的问题:这个问题的最佳解决方案是什么?
I guess one way would be to place a private enum field inside each Student class and have 4 if-else statements checking what type of Student it is and then creating a new dynamic object based on that like so: 我想有一种方法是在每个Student类中放置一个私有枚举字段,并有4个if-else语句检查它是什么类型的Student,然后根据它创建一个新的动态对象:
for(int i = 0; i < students.size(); i++) {
if(students.at(i).getType() == FRESHMAN) {
this->students.at(i) = new Freshman(*students.at(i));
} else if(students.at(i).getType() == SOPHMORE) {
this->students.at(i) = new Sophmore(*students.at(i));
} else if {
// and so on...
}
}
But this still seems quite cumbersome, so what would you suggest? 但这仍然看起来很麻烦,你会建议什么?
You're looking for the Clone pattern. 你正在寻找克隆模式。 Add a clone() virtual function to Student, which is overridden in each descendant and creates the appropriate copy.
向Student添加clone()虚函数,在每个后代中重写并创建相应的副本。 Then write deep copy of containers as you've correctly specified.
然后按照您正确指定的方式编写容器的深层副本。
Edit: my work assumption is that your Freshman, etc. classes are descending from Student. 编辑:我的工作假设是你的新生等班级来自学生。 If not, use a variant<> and apply a copy visitor.
如果没有,请使用变体<>并应用副本访问者。
Resolving ownership issues 解决所有权问题
If you deem to shared the Student
-s between your modules, then you are facing an ownership issue, and I would recommend using vector of std::shared_ptr<Student>
-s to solve it. 如果您希望在模块之间共享
Student
,那么您将面临所有权问题,我建议使用std::shared_ptr<Student>
-s的向量来解决它。
If you have a std::vector<std::shared_ptr<Student>>
, you can pass it to anyone you want. 如果你有一个
std::vector<std::shared_ptr<Student>>
,你可以将它传递给任何你想要的人。 The receiver can copy the vector
using an assignment operator and later on any objects he adds/removes won't affect the original container, just like you seem to desire. 接收器可以使用赋值运算符复制
vector
,稍后他添加/删除的任何对象都不会影响原始容器,就像您希望的那样。
Resolving cloning issues 解决克隆问题
If you are interested in each module having its own copy of a Student
-s vector, you are facing a cloning issue. 如果您对每个具有自己的
Student
-s向量副本的模块感兴趣,那么您将面临克隆问题。
You could resolve it by adding the following method to your classes: 您可以通过向您的类添加以下方法来解决它:
class Student {
[..]
virtual Student * clone() const = 0; // Assuming Student is abstract, otherwise implement as well
};
class Freshman : public Student {
[..]
virtual Freshman * clone() const { return new Freshman(*this); }
};
// Same for other derived classes...
And then using std::transform
to copy the vector: 然后使用
std::transform
复制向量:
// students is the original std::vector<Student *>
std::vector<Student *> copy(students.size());
std::transform(students.begin(), students.end(), copy.begin(), [](Student * s) -> Student * { return s->clone(); });
BTW, it's Sophomore, not Sophmore... 顺便说一句,这是二年级学生,不是索菲尔......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.