相关疑难解决方法(0)

以允许响应更新的方式重载C++索引下标operator []

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

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

我有什么方法可以区分被访问的值和被修改的值?

Type a = myIndexable[2]; //Access
myIndexable[3] = a;  //Modification
Run Code Online (Sandbox Code Playgroud)

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

c++ indexing operator-overloading

25
推荐指数
3
解决办法
4万
查看次数

标签 统计

c++ ×1

indexing ×1

operator-overloading ×1