久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

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

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

      1. <tfoot id='yZUK4'></tfoot>

        帶有模板成員變量的 C++ 類

        C++ class with template member variable(帶有模板成員變量的 C++ 類)
        • <i id='KU36n'><tr id='KU36n'><dt id='KU36n'><q id='KU36n'><span id='KU36n'><b id='KU36n'><form id='KU36n'><ins id='KU36n'></ins><ul id='KU36n'></ul><sub id='KU36n'></sub></form><legend id='KU36n'></legend><bdo id='KU36n'><pre id='KU36n'><center id='KU36n'></center></pre></bdo></b><th id='KU36n'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='KU36n'><tfoot id='KU36n'></tfoot><dl id='KU36n'><fieldset id='KU36n'></fieldset></dl></div>

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

            <tfoot id='KU36n'></tfoot>
              • <bdo id='KU36n'></bdo><ul id='KU36n'></ul>

                • <legend id='KU36n'><style id='KU36n'><dir id='KU36n'><q id='KU36n'></q></dir></style></legend>

                    <tbody id='KU36n'></tbody>

                  本文介紹了帶有模板成員變量的 C++ 類的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  限時送ChatGPT賬號..

                  我正在嘗試解決一個編程問題,該問題由一個包含多個參數的對象(稱為圖)組成.每個參數(Parameter 類)可以是以下幾種類型之一:int、double、complex、string - 僅舉幾例.

                  I am trying to solve a programming problem that consists of an object (call it Diagram), that contains several parameters. Each parameter (the Parameter class) can be one of several types: int, double, complex, string - to name a few.

                  所以我的第一反應是將我的 Diagram 類定義為具有模板參數的向量,看起來像這樣.

                  So my first instinct was to define my Diagram class as having a vector of template parameters, which would look like this.

                  class Diagram
                  {
                  private:
                      std::vector<Parameter<T> > v;
                  };
                  

                  這不能編譯,我明白為什么.因此,基于此頁面上的建議 如何聲明屬于類中任何類型對象的數據成員,我將代碼修改為:

                  This doesn't compile, and I understand why. So, based on the recommendations on this page How to declare data members that are objects of any type in a class, I modified my code to look like:

                  class ParameterBase
                  {
                  public:
                      virtual void setValue() = 0;
                      virtual ~ParameterBase() { }
                  };
                  
                  
                  template <typename T>
                  class Parameter : public ParameterBase
                  {
                  public:
                      void setValue() // I want this to be 
                                      // void setValue(const T & val)
                      {
                          // I want this to be 
                          // value = val;
                      }
                  
                  private:
                      T value;
                  };
                  
                  class Diagram
                  {
                  public:
                      std::vector<ParameterBase *> v;
                      int type;
                  };
                  

                  我無法弄清楚如何使用適當的模板化參數調用 setValue 函數.在 ParameterBase 抽象基類中不可能有模板化參數.非常感謝任何幫助.

                  I'm having trouble figuring out how to call the setValue function with an appropriate templated parameter. It is not possible to have a templated parameter in the ParameterBase abstract base class. Any help is greatly appreciated.

                  附言我沒有使用 boost::any 的靈活性.

                  P.S. I don't have the flexibility to use boost::any.

                  推薦答案

                  你們已經很接近了.我添加了一些,因為它們很方便

                  You got very close. I added a few bits because they're handy

                  class ParameterBase
                  {
                  public:
                      virtual ~ParameterBase() {}
                      template<class T> const T& get() const; //to be implimented after Parameter
                      template<class T, class U> void setValue(const U& rhs); //to be implimented after Parameter
                  };
                  
                  template <typename T>
                  class Parameter : public ParameterBase
                  {
                  public:
                      Parameter(const T& rhs) :value(rhs) {}
                      const T& get() const {return value;}
                      void setValue(const T& rhs) {value=rhs;}    
                  private:
                      T value;
                  };
                  
                  //Here's the trick: dynamic_cast rather than virtual
                  template<class T> const T& ParameterBase::get() const
                  { return dynamic_cast<const Parameter<T>&>(*this).get(); }
                  template<class T, class U> void ParameterBase::setValue(const U& rhs)
                  { return dynamic_cast<Parameter<T>&>(*this).setValue(rhs); }
                  
                  class Diagram
                  {
                  public:
                      std::vector<ParameterBase*> v;
                      int type;
                  };
                  

                  然后圖表可以做這樣的事情:

                  Diagram can then do stuff like these:

                  Parameter<std::string> p1("Hello");
                  v.push_back(&p1);
                  std::cout << v[0]->get<std::string>(); //read the string
                  v[0]->set<std::string>("BANANA"); //set the string to something else
                  v[0]->get<int>(); //throws a std::bad_cast exception
                  

                  看起來您的意圖是將擁有資源的指針存儲在向量中.如果是這樣,請注意使 Diagram 具有正確的析構函數,并使其不可復制構造,不可復制賦值.

                  It looks like your intent is to store resource-owning pointers in the vector. If so, be careful to make Diagram have the correct destructor, and make it non-copy-constructable, and non-copy-assignable.

                  這篇關于帶有模板成員變量的 C++ 類的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  Why do two functions have the same address?(為什么兩個函數的地址相同?)
                  Why the initializer of std::function has to be CopyConstructible?(為什么 std::function 的初始化程序必須是可復制構造的?)
                  mixing templates with polymorphism(混合模板與多態性)
                  When should I use the keyword quot;typenamequot; when using templates(我什么時候應該使用關鍵字“typename?使用模板時)
                  Dependent name resolution amp; namespace std / Standard Library(依賴名稱解析命名空間 std/標準庫)
                  gcc can compile a variadic template while clang cannot(gcc 可以編譯可變參數模板,而 clang 不能)

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

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

                          <tfoot id='EXMdI'></tfoot>
                            <tbody id='EXMdI'></tbody>
                          <legend id='EXMdI'><style id='EXMdI'><dir id='EXMdI'><q id='EXMdI'></q></dir></style></legend>
                          • 主站蜘蛛池模板: 国产女人与拘做视频免费 | 免费av一区二区三区 | 久久国产精99精产国高潮 | 在线视频中文字幕 | 美女操网站 | 黄色一级大片在线免费看产 | 99pao成人国产永久免费视频 | 99久久免费精品国产男女高不卡 | 中文成人无字幕乱码精品 | 中国大陆高清aⅴ毛片 | 久久久久亚洲精品 | 精品国产一区二区三区久久狼黑人 | 成人精品一区二区三区中文字幕 | 欧美男人天堂 | 综合在线视频 | 欧美精品久久久久久久久老牛影院 | 亚洲高清在线播放 | 国产精品福利在线 | 一级毛片视频在线观看 | 色综合久久久久 | 宅男伊人 | 国产高清视频在线 | 午夜丁香视频在线观看 | 中文字幕一区二区三区四区五区 | 精品亚洲一区二区三区 | 国产高清在线精品一区二区三区 | 天天影视综合 | 日韩一区二区在线视频 | 久久区二区 | 日韩视频在线观看 | 久久成人精品视频 | 中文一区 | 欧美国产日韩在线观看成人 | 国产精品久久久 | 国产激情视频网 | 日本黄视频在线观看 | 一道本不卡视频 | av一级一片 | 欧美日韩在线成人 | 成人字幕网zmw | 一级毛片在线看 |