这是我的代码段:
inline uint8_t &AT(uint16_t addr) { // non-const version
return (addr < 0x2000 ? Ram[addr & 0x7ff]: \
addr < 0x4000 ? PPURegister[(addr - 0x2000) & 0x7]: \
addr < 0x4020 ? APUIORegister[(addr - 0x4000)]: \
addr < 0x6000 ? ExpansionRom[addr - 0x4020]: \
addr < 0x8000 ? SRam[addr - 0x6000]: \
addr < 0xc000 ? LowerPRGRom[addr - 0x8000]: \
UpperPRGRom[addr - 0xc000]);
}
inline const uint8_t &AT(uint16_t addr) const { // const version
return AT(addr); // Function `AT` will recurses infinitely! But I don't want to copy the body of non-const function here.
}
有些功能使用它:
uint8_t &operator[](uint16_t addr) { return AT(addr); }
const uint8_t &operator[](uint16_t addr) const { return AT(addr); }
那么,如何实现简单明了地返回const的函数呢?