• <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 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>

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

      2. 带有模板成员变量的 C++ 类

        时间:2023-05-25

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

          <tbody id='yZUK4'></tbody>
          <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 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>

                • <tfoot id='yZUK4'></tfoot>
                  本文介绍了带有模板成员变量的 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模板网!

                  上一篇:C++模板参数类型推断 下一篇:模板元编程 - 使用 Enum Hack 和 Static Const 的区别

                  相关文章

                  最新文章

                        <bdo id='EXMdI'></bdo><ul id='EXMdI'></ul>
                    1. <small id='EXMdI'></small><noframes id='EXMdI'>

                    2. <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 id='EXMdI'><tfoot id='EXMdI'></tfoot><dl id='EXMdI'><fieldset id='EXMdI'></fieldset></dl></div>

                      <tfoot id='EXMdI'></tfoot>
                    3. <legend id='EXMdI'><style id='EXMdI'><dir id='EXMdI'><q id='EXMdI'></q></dir></style></legend>