为 boost 指针容器调用基类的复制构造函数?

发布于 2024-10-31 11:21:38 字数 1475 浏览 2 评论 0原文

对于下面的代码,当复制 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

勿忘心安 2024-11-07 11:21:38

(1) 要调用模型复制构造函数,请更改 ModelInherit 复制构造函数,如下所示:

ModelInherit(const ModelInherit& m): Model(m), j(m.j) {}

(2) 可以像这样完成深层复制:

Model(const Model& m): i(m.i), s(0)
{
  if(m.s != 0)
    this->s = new SomeNewClass(*(m.s));
  cout<<"Model Copy ctor invoked"<<endl;
}

并为 SomeNewClass 声明一个复制构造函数,如下所示:

SomeNewClass(const SomeNewClass ©) : a(copy.a)
{
  cout<<"SomeNewClass Copy ctor invoked"<<endl;
}

不要忘记释放 Model:: s 在析构函数中,否则会泄漏内存:

~Model () { delete this->s; }  // it's ok if s = 0

(1) To invoke Model copy constructor, change your ModelInherit copy constructor like following:

ModelInherit(const ModelInherit& m): Model(m), j(m.j) {}

(2) Deep copy can be done like this:

Model(const Model& m): i(m.i), s(0)
{
  if(m.s != 0)
    this->s = new SomeNewClass(*(m.s));
  cout<<"Model Copy ctor invoked"<<endl;
}

And declare a copy constructor for SomeNewClass like below:

SomeNewClass(const SomeNewClass ©) : a(copy.a)
{
  cout<<"SomeNewClass Copy ctor invoked"<<endl;
}

Don't forget to free Model::s in destructor, otherwise it will leak memory:

~Model () { delete this->s; }  // it's ok if s = 0
失而复得 2024-11-07 11:21:38

调用基类复制构造函数很简单:

ModelInherit(const ModelInherit& m): Model(m), j(m.j) {}
                                  //^^^^^^^^ note this

Model(m) 调用基类复制构造函数;参数m隐式转换为基类。

在基类复制构造函数中,您必须手动深度复制 ms

Invoking base class copy-constructor is easy:

ModelInherit(const ModelInherit& m): Model(m), j(m.j) {}
                                  //^^^^^^^^ note this

Model(m) invokes base class copy-constructor; the parameter m implicitly converts into base class.

In the base class copy-constructor, you've to manually deep-copy m.s.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文