Sag*_*Low 52 c++ indexing overloading square-bracket
我有以下课程:
class risc { // singleton
protected:
static unsigned long registers[8];
public:
unsigned long operator [](int i)
{
return registers[i];
}
};
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,我已经实现了方括号运算符"get".
现在我想实现它的设置,即:risc[1] = 2.
怎么做到呢?
And*_*ard 71
试试这个:
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];}
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
55839 次 |
| 最近记录: |