<tfoot id='PkEZF'></tfoot>

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

        <bdo id='PkEZF'></bdo><ul id='PkEZF'></ul>

        <small id='PkEZF'></small><noframes id='PkEZF'>

        如何在 C++ 中克隆对象?或者有其他解决方案吗?

        How to clone object in C++ ? Or Is there another solution?(如何在 C++ 中克隆对象?或者有其他解决方案吗?)
        <legend id='CcHqP'><style id='CcHqP'><dir id='CcHqP'><q id='CcHqP'></q></dir></style></legend>
          <tbody id='CcHqP'></tbody>

          <small id='CcHqP'></small><noframes id='CcHqP'>

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

                • 本文介绍了如何在 C++ 中克隆对象?或者有其他解决方案吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我写了一个堆栈和队列实现(基于链表).有一个堆栈(bigStack).例如,我将 bigStack 分开(例如:stackAstackB).我 pop() 一个来自 bigStack 的节点,我 push()stackA 中.同样,我在stackBpush().我希望 bigStack 不会改变.因此我想克隆 bigStack 对象.如何在 C++ 中克隆对象?或者我的问题还有其他解决方案吗?

                  I wrote a Stack and Queue implementation (Linked List based). There is one stack (bigStack). For example, I separate bigStack (example: stackA and stackB). I pop() a node from bigStack, I push() in stackA. In the same way, I push() in stackB. I want bigStack to not change. Therefore I want to clone the bigStack object. How do I clone objects in C++? Or is there another solution to my problem?

                  class Stack : public List {
                  public:
                     Stack() {}
                     Stack(const Stack& rhs) {}
                     Stack& operator=(const Stack& rhs) {};
                      ~Stack() {}
                  
                      int Top() {
                          if (head == NULL) {
                              cout << "Error: The stack is empty." << endl;
                              return -1;
                          } else {
                              return head->nosu;
                          }
                      }
                  
                      void Push(int nosu, string adi, string soyadi, string bolumu) {
                          InsertNode(0, nosu, adi, soyadi, bolumu);
                      }
                  
                      int Pop() {
                          if (head == NULL) {
                              cout << "Error: The stack is empty." << endl;
                              return -1;
                          } else {
                              int val = head->nosu;
                              DeleteNode(val);
                              return val;
                          }
                      }
                  
                      void DisplayStack(void);
                  
                  };
                  

                  然后...

                  Stack copyStack = veriYapilariDersi;
                  copyStack.DisplayStack();
                  

                  推荐答案

                  对此的典型解决方案是编写自己的函数来克隆对象.如果您能够提供复制构造函数和复制赋值运算符,这可能是您需要的.

                  The typical solution to this is to write your own function to clone an object. If you are able to provide copy constructors and copy assignement operators, this may be as far as you need to go.

                  class Foo
                  { 
                  public:
                    Foo();
                    Foo(const Foo& rhs) { /* copy construction from rhs*/ }
                    Foo& operator=(const Foo& rhs) {};
                  };
                  
                  // ...
                  
                  Foo orig;
                  Foo copy = orig;  // clones orig if implemented correctly
                  

                  有时提供显式的 clone() 方法是有益的,尤其是对于多态类.

                  Sometimes it is beneficial to provide an explicit clone() method, especially for polymorphic classes.

                  class Interface
                  {
                  public:
                    virtual Interface* clone() const = 0;
                  };
                  
                  class Foo : public Interface
                  {
                  public:
                    Interface* clone() const { return new Foo(*this); }
                  };
                  
                  class Bar : public Interface
                  {
                  public:
                    Interface* clone() const { return new Bar(*this); }
                  };
                  
                  
                  Interface* my_foo = /* somehow construct either a Foo or a Bar */;
                  Interface* copy = my_foo->clone();
                  

                  由于 Stack 没有成员变量,所以在复制构造函数或复制赋值运算符中没有任何事情可以从所谓的右手边"(rhs).但是,您仍然需要确保任何基类都有机会初始化它们的 成员.

                  Since Stack has no member variables, there's nothing to do in the copy constructor or copy assignment operator to initialize Stack's members from the so-called "right hand side" (rhs). However, you still need to ensure that any base classes are given the opportunity to initialize their members.

                  您可以通过调用基类来实现:

                  You do this by calling the base class:

                  Stack(const Stack& rhs) 
                  : List(rhs)  // calls copy ctor of List class
                  {
                  }
                  
                  Stack& operator=(const Stack& rhs) 
                  {
                    List::operator=(rhs);
                    return * this;
                  };
                  

                  这篇关于如何在 C++ 中克隆对象?或者有其他解决方案吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Consistent pseudo-random numbers across platforms(跨平台一致的伪随机数)
                  Vary range of uniform_int_distribution(改变uniform_int_distribution的范围)
                  What is a seed in terms of generating a random number?(就生成随机数而言,种子是什么?)
                  Is 1.0 a valid output from std::generate_canonical?(1.0 是 std::generate_canonical 的有效输出吗?)
                  Getting big random numbers in C/C++(在 C/C++ 中获取大随机数)
                  What is the best way to generate random numbers in C++?(在 C++ 中生成随机数的最佳方法是什么?)
                  <legend id='xdIP6'><style id='xdIP6'><dir id='xdIP6'><q id='xdIP6'></q></dir></style></legend>
                  <i id='xdIP6'><tr id='xdIP6'><dt id='xdIP6'><q id='xdIP6'><span id='xdIP6'><b id='xdIP6'><form id='xdIP6'><ins id='xdIP6'></ins><ul id='xdIP6'></ul><sub id='xdIP6'></sub></form><legend id='xdIP6'></legend><bdo id='xdIP6'><pre id='xdIP6'><center id='xdIP6'></center></pre></bdo></b><th id='xdIP6'></th></span></q></dt></tr></i><div id='xdIP6'><tfoot id='xdIP6'></tfoot><dl id='xdIP6'><fieldset id='xdIP6'></fieldset></dl></div>

                    <small id='xdIP6'></small><noframes id='xdIP6'>

                        <tbody id='xdIP6'></tbody>

                        <bdo id='xdIP6'></bdo><ul id='xdIP6'></ul>
                            <tfoot id='xdIP6'></tfoot>