为 boost 指针容器调用基类的复制构造函数?
对于下面的代码,当复制 v 时,模型类的成员不会被复制。
#include <boost/ptr_container/ptr_vector.hpp>
#include <iostream>
using namespace std;
class SomeNewClass
{
public:
int a;
};
class Model
{
public:
int i;
SomeNewClass* s;//A deep copy won't happen here automatically
Model() {}
Model(const Model& m):i(m.i)
{
cout<<"Model Copy ctor invoked"<<endl;
}
};
class ModelInherit : public Model
{
public:
int j;
ModelInherit() {}
ModelInherit(const ModelInherit& m):j(m.j)
{
//i=m.i;//I don't want to copy like this. I want the copy ctor of Model to be invoked
cout<<"ModelInherit Copy ctor invoked"<<endl;
}
};
int main()
{
boost::ptr_vector<ModelInherit> v;
v.push_back(new ModelInherit);
v[0].j = 10;
v[0].i = 20;
v[0].s = new SomeNewClass();
v[0].s->a = 99;
boost::ptr_vector<ModelInherit> v2( v );
cout<< v2[0].j <<endl;
cout<< v2[0].i <<endl;
//cout<< v2[0].s->a <<endl;//segmentation fault
}
需要注意的是,如果注释掉ModelInherit的复制构造函数,那么指针容器会自动复制Model类中的i变量。可悲的是“SomeNewClass* s”没有被复制。没有深拷贝。
所以我的问题是:
- 你知道如何调用副本吗 Model 类的构造函数 上面的代码?
- 当指针容器自动复制变量时,如何确保深度复制,甚至 SomeNewClass 的“a”变量也会被复制?
For the below code, when v is copied, the members of Model class do not get copied.
#include <boost/ptr_container/ptr_vector.hpp>
#include <iostream>
using namespace std;
class SomeNewClass
{
public:
int a;
};
class Model
{
public:
int i;
SomeNewClass* s;//A deep copy won't happen here automatically
Model() {}
Model(const Model& m):i(m.i)
{
cout<<"Model Copy ctor invoked"<<endl;
}
};
class ModelInherit : public Model
{
public:
int j;
ModelInherit() {}
ModelInherit(const ModelInherit& m):j(m.j)
{
//i=m.i;//I don't want to copy like this. I want the copy ctor of Model to be invoked
cout<<"ModelInherit Copy ctor invoked"<<endl;
}
};
int main()
{
boost::ptr_vector<ModelInherit> v;
v.push_back(new ModelInherit);
v[0].j = 10;
v[0].i = 20;
v[0].s = new SomeNewClass();
v[0].s->a = 99;
boost::ptr_vector<ModelInherit> v2( v );
cout<< v2[0].j <<endl;
cout<< v2[0].i <<endl;
//cout<< v2[0].s->a <<endl;//segmentation fault
}
What is important to note is that if you comment out the copy constructor of ModelInherit, then the pointer container automatically copies the i variable in the Model class. Sad part is that "SomeNewClass* s" does not get copied. No deep copy.
So my questions are:
- Do you know how to invoke the copy
constructor of the Model class in the
above code? - How do I ensure a deep copy when the pointer container is automatically copying variables so that even the 'a' variable of SomeNewClass gets copied?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
(1) 要调用模型复制构造函数,请更改 ModelInherit 复制构造函数,如下所示:
(2) 可以像这样完成深层复制:
并为 SomeNewClass 声明一个复制构造函数,如下所示:
不要忘记释放
Model:: s
在析构函数中,否则会泄漏内存:(1) To invoke Model copy constructor, change your ModelInherit copy constructor like following:
(2) Deep copy can be done like this:
And declare a copy constructor for SomeNewClass like below:
Don't forget to free
Model::s
in destructor, otherwise it will leak memory:调用基类复制构造函数很简单:
Model(m)
调用基类复制构造函数;参数m
隐式转换为基类。在基类复制构造函数中,您必须手动深度复制
ms
。Invoking base class copy-constructor is easy:
Model(m)
invokes base class copy-constructor; the parameterm
implicitly converts into base class.In the base class copy-constructor, you've to manually deep-copy
m.s
.