重载括号运算符 [] 以获取和设置
我有以下课程:
class risc { // singleton
protected:
static unsigned long registers[8];
public:
unsigned long operator [](int i)
{
return registers[i];
}
};
如您所见,我已经为获取"实现了方括号运算符.
现在我想实现它进行设置,即:risc[1] = 2
.
as you can see I've implemented the square brackets operator for "getting".
Now I would like to implement it for setting, i.e.: risc[1] = 2
.
怎么做?
推荐答案
试试这个:
class risc { // singleton
protected:
static unsigned long registers[8];
public:
unsigned long operator [](int i) const {return registers[i];}
unsigned long & operator [](int i) {return registers[i];}
};
相关文章