Overloading the C++ indexing subscript operator [] in a manner that allows for responses to updates

后端 未结 6 1235
我在风中等你
我在风中等你 2020-12-08 10:32

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 n

6条回答
  •  孤街浪徒
    2020-12-08 10:46

    You can have (the non-const) operator[] return a proxy object that keeps a reference or pointer to the container, and in which operator= signals the container of the update.

    (The idea of using const vs non-const operator[] is a red herring... you may know that you've just given away non-const access to the object, but you don't know if that access is still being used for a read or a write, when that write completes, or have any mechanism for updating the container thereafter.)

提交回复
热议问题