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

    <tfoot id='16vMU'></tfoot>

        <bdo id='16vMU'></bdo><ul id='16vMU'></ul>

      <small id='16vMU'></small><noframes id='16vMU'>

    1. <legend id='16vMU'><style id='16vMU'><dir id='16vMU'><q id='16vMU'></q></dir></style></legend>
      1. 以允许响应更新的方式重载 C++ 索引下标运算符 []

        Overloading the C++ indexing subscript operator [] in a manner that allows for responses to updates(以允许响应更新的方式重载 C++ 索引下标运算符 [])
          <tbody id='v06Tm'></tbody>
        <legend id='v06Tm'><style id='v06Tm'><dir id='v06Tm'><q id='v06Tm'></q></dir></style></legend>

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

          • <tfoot id='v06Tm'></tfoot>
          • <small id='v06Tm'></small><noframes id='v06Tm'>

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

                  本文介绍了以允许响应更新的方式重载 C++ 索引下标运算符 []的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  考虑编写一个可索引类的任务,该类自动将其状态与某些外部数据存储(例如文件)同步.为了做到这一点,类需要知道可能发生的对索引值的更改.不幸的是,重载 operator[] 的常用方法不允许这样做,例如...

                  Consider the task of writing an indexable class which automatically synchronizes its state with some external data-store (e.g. a file). In order to do this the class would need to be made aware of changes to the indexed value which might occur. Unfortunately the usual approach to overloading operator[] does not allow for this, for example...

                  Type& operator[](int index)
                  {
                      assert(index >=0 && index < size);
                      return state[index];
                  }
                  

                  有什么方法可以区分正在访问的值和正在修改的值吗?

                  I there any way to distinguish between a value being accessed and a value being modified?

                  Type a = myIndexable[2]; //Access
                  myIndexable[3] = a;  //Modification
                  

                  这两种情况都发生在函数返回之后.是否有其他一些方法来重载 operator[] 可能更有意义?

                  Both of these cases occur after the function has returned. Is there some other approach to overloading operator[] which would perhaps make more sense?

                  推荐答案

                  从操作员[]你只能真正告诉访问.
                  即使外部实体使用非成本版本,这也并不意味着写入将发生,而是可能发生.

                  From the operator[] you can only really tell access.
                  Even if the external entity uses the non cost version this does not mean that a write will take place rather that it could take place.

                  因此,您需要做的是返回一个可以检测修改的对象.
                  最好的方法是用一个覆盖operator= 的类包装对象.当对象被更新时,这个包装器然后可以通知商店.您还需要覆盖 operator Type(强制转换),以便可以检索对象的 const 版本以进行读取访问.

                  As such What you need to do is return an object that can detect modification.
                  The best way to do this is to wrap the object with a class that overrides the operator=. This wrapper can then inform the store when the object has been updated. You would also want to override the operator Type (cast) so that a const version of the object can be retrieved for read accesses.

                  然后我们可以这样做:

                  class WriteCheck;
                  class Store
                  {
                    public:
                    Type const& operator[](int index) const
                    {
                      return state[index];
                    } 
                    WriteCheck operator[](int index);
                    void stateUpdate(int index)
                    {
                          // Called when a particular index has been updated.
                    }
                    // Stuff
                  };
                  
                  class WriteCheck
                  { 
                      Store&  store;
                      Type&   object;
                      int     index;
                  
                      public: WriteCheck(Store& s, Type& o, int i): store(s), object(o), index(i) {}
                  
                      // When assignment is done assign
                      // Then inform the store.
                      WriteCheck& operator=(Type const& rhs)
                      {
                          object = rhs;
                          store.stateUpdate(index);
                      }
                  
                      // Still allow the base object to be read
                      // From within this wrapper.
                      operator Type const&()
                      {
                          return object;
                      }   
                  };      
                  
                  WriteCheck Store::operator[](int index)
                  {   
                      return WriteCheck(*this, state[index], index);
                  }
                  

                  一个更简单的选择是:
                  您没有提供 operator[],而是在 store 对象上提供特定的 set 方法,并且仅通过 operator[]

                  An simpler alternative is:
                  Rather than provide the operator[] you provide a specific set method on the store object and only provide read access through the operator[]

                  这篇关于以允许响应更新的方式重载 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++ 中生成随机数的最佳方法是什么?)
                      <tfoot id='h5HGc'></tfoot>

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

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