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

      <small id='7GhD0'></small><noframes id='7GhD0'>

      • <bdo id='7GhD0'></bdo><ul id='7GhD0'></ul>
      1. <legend id='7GhD0'><style id='7GhD0'><dir id='7GhD0'><q id='7GhD0'></q></dir></style></legend>

      2. C++:模板类的向量

        时间:2023-09-15
          <tbody id='UG8wo'></tbody>
      3. <small id='UG8wo'></small><noframes id='UG8wo'>

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

                1. 本文介绍了C++:模板类的向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一个名为 Cell 的模板类,如下所示:-

                  I have a template class named Cell as follows:-

                  template<class T>class Cell
                  {
                      string header, T data;
                  }
                  

                  现在我想要另一个名为 Row 的类.Row 将有一个名为 Cells 的向量,这样我就可以将 Cell 和 Cell 类型元素添加到该向量中.可能吗?

                  Now I want another class Named Row. Row will have a vector named Cells such that I can add both Cell and Cell type elements to that vector. Is it possible?

                  如果是这样,我该怎么做?提前致谢.

                  If so, how can I do that? Thanks in advance.

                  推荐答案

                  根据您提供的额外细节,前两个答案将不起作用.您需要的是一种称为细胞变体的类型,然后您可以拥有这些类型的向量.例如:-

                  With the extra detail you've provided, the first two answers won't work. What you require is a type known as a variant for the cell and then you can have a vector of those. For example:-

                  enum CellType
                  {
                    Int,
                    Float,
                    // etc
                  };
                  
                  class Cell
                  {
                    CellType type;
                    union
                    {
                      int i;
                      float f;
                      // etc
                    };
                  };
                  
                  class Vector
                  {
                    vector <Cell> cells;
                  };
                  

                  然而,添加新类型很痛苦,因为它需要大量代码来维护.另一种方法可以使用具有公共基类的单元格模板:-

                  This, however, is a pain to add new types to as it requires a lot of code to maintain. An alternative could use the cell template with a common base class:-

                  class ICell
                  {
                    // list of cell methods
                  };
                  
                  template <class T>
                  class Cell : public ICell
                  {
                    T data;
                    // implementation of cell methods
                  };
                  
                  class Vector
                  {
                    vector <ICell *> cells;
                  };
                  

                  这可能会更好,因为您最初需要更新的代码较少以添加新的单元格类型,但您必须在单元格向量中使用指针类型.如果您按值存储单元格,vector ,那么您将由于 对象切片而丢失数据.

                  This might work better as you have less code initially to update to add a new cell type but you have to use a pointer type in the cells vector. If you stored the cell by value, vector <ICell>, then you will lose data due to object slicing.

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

                  上一篇:如何将字符串向量传递给 execv 下一篇:初始化列表构造不可复制(但可移动)对象的向量

                  相关文章

                  最新文章

                2. <small id='iQ8tl'></small><noframes id='iQ8tl'>

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

                  <tfoot id='iQ8tl'></tfoot>