• <i id='3Jo6I'><tr id='3Jo6I'><dt id='3Jo6I'><q id='3Jo6I'><span id='3Jo6I'><b id='3Jo6I'><form id='3Jo6I'><ins id='3Jo6I'></ins><ul id='3Jo6I'></ul><sub id='3Jo6I'></sub></form><legend id='3Jo6I'></legend><bdo id='3Jo6I'><pre id='3Jo6I'><center id='3Jo6I'></center></pre></bdo></b><th id='3Jo6I'></th></span></q></dt></tr></i><div id='3Jo6I'><tfoot id='3Jo6I'></tfoot><dl id='3Jo6I'><fieldset id='3Jo6I'></fieldset></dl></div>

    <small id='3Jo6I'></small><noframes id='3Jo6I'>

    • <bdo id='3Jo6I'></bdo><ul id='3Jo6I'></ul>
    <tfoot id='3Jo6I'></tfoot>

  • <legend id='3Jo6I'><style id='3Jo6I'><dir id='3Jo6I'><q id='3Jo6I'></q></dir></style></legend>
      1. C++:深度复制基类指针

        时间:2023-08-02
          <tbody id='K8f7x'></tbody>
        <legend id='K8f7x'><style id='K8f7x'><dir id='K8f7x'><q id='K8f7x'></q></dir></style></legend>

            <tfoot id='K8f7x'></tfoot>

            • <bdo id='K8f7x'></bdo><ul id='K8f7x'></ul>
                • <small id='K8f7x'></small><noframes id='K8f7x'>

                  <i id='K8f7x'><tr id='K8f7x'><dt id='K8f7x'><q id='K8f7x'><span id='K8f7x'><b id='K8f7x'><form id='K8f7x'><ins id='K8f7x'></ins><ul id='K8f7x'></ul><sub id='K8f7x'></sub></form><legend id='K8f7x'></legend><bdo id='K8f7x'><pre id='K8f7x'><center id='K8f7x'></center></pre></bdo></b><th id='K8f7x'></th></span></q></dt></tr></i><div id='K8f7x'><tfoot id='K8f7x'></tfoot><dl id='K8f7x'><fieldset id='K8f7x'></fieldset></dl></div>
                • 本文介绍了C++:深度复制基类指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我四处搜索,似乎为了执行此操作,我需要更改我的基类,并想知道这是否是最佳方法.例如,我有一个基类:

                  I searched around and seems in order to perform this I need to change my Base class and want to know if this is the best approach. For example, I have a Base class:

                  class Base {}
                  

                  然后是一长串派生类:

                  class Derived_1:: public Base {}
                  class Derived_2:: public Derived_1{}
                  ...
                  ...
                  class Derived_n:: public Derived_M{}
                  

                  然后我又上了一节课:

                  class DeepCopy 
                  { 
                    Base * basePtr;
                  
                    public:
                     DeepCopy(DeepCopy & dc) {}
                  }
                  

                  假设 Base 类和 Derived_x 类复制构造函数编码正确,那么为 DeepCopy 编写复制构造函数的最佳方法是什么.我们如何知道我们要复制的对象的 basePtr 中的类?

                  Assuming the Base class and Derived_x class copy constructors are properly coded, what is the best way to write the copy constructor for DeepCopy. How can we know about the class that is in the basePtr of the object we are going to copy?

                  我能想到的唯一方法是使用 RTTI,但使用一长串 dynamic_casts 似乎不对.另外需要DeepCopy知道Base类的继承层次.

                  Only way I can think of is using RTTI, but using a long list of dynamic_casts seems not right. Besides it requires DeepCopy to know about the inheritance hierarchy of Base class.

                  我看到的另一种方法是这里.但它需要 Base 和 Derived 类实现克隆方法.

                  The other method I saw is here. But it requires Base and Derived classes implement a clone method.

                  那么有没有更简单、标准的方法来做到这一点?

                  So is there a much easier, standard way of doing this?

                  推荐答案

                  你需要使用virtual copy 模式:在接口中提供一个虚函数来进行复制,然后跨平台实现层次结构:

                  You need to use the virtual copy pattern: provide a virtual function in the interface that does the copy and then implement it across the hierarchy:

                  struct base {
                     virtual ~base() {}                // Remember to provide a virtual destructor
                     virtual base* clone() const = 0;
                  };
                  struct derived : base {
                     virtual derived* clone() const {
                        return new derived(*this);
                     }
                  };
                  

                  然后 DeepCopy 对象只需要调用该函数:

                  Then the DeepCopy object just needs to call that function:

                  class DeepCopy 
                  { 
                    Base * basePtr;    
                  public:
                     DeepCopy(DeepCopy const & dc)           // This should be `const`
                        : basePtr( dc.basePtr->clone() )
                     {}
                  };
                  

                  这篇关于C++:深度复制基类指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何在 C++ 中定义密封类? 下一篇:仅使用基类指针复制派生实体,(无需详尽测试!

                  相关文章

                  最新文章

                  1. <tfoot id='Q5Gj0'></tfoot>
                  2. <small id='Q5Gj0'></small><noframes id='Q5Gj0'>

                      <i id='Q5Gj0'><tr id='Q5Gj0'><dt id='Q5Gj0'><q id='Q5Gj0'><span id='Q5Gj0'><b id='Q5Gj0'><form id='Q5Gj0'><ins id='Q5Gj0'></ins><ul id='Q5Gj0'></ul><sub id='Q5Gj0'></sub></form><legend id='Q5Gj0'></legend><bdo id='Q5Gj0'><pre id='Q5Gj0'><center id='Q5Gj0'></center></pre></bdo></b><th id='Q5Gj0'></th></span></q></dt></tr></i><div id='Q5Gj0'><tfoot id='Q5Gj0'></tfoot><dl id='Q5Gj0'><fieldset id='Q5Gj0'></fieldset></dl></div>
                        <bdo id='Q5Gj0'></bdo><ul id='Q5Gj0'></ul>

                    1. <legend id='Q5Gj0'><style id='Q5Gj0'><dir id='Q5Gj0'><q id='Q5Gj0'></q></dir></style></legend>