1

Here is my code snippet:

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.
}

There are functions uses it:

uint8_t &operator[](uint16_t addr) { return AT(addr); }
const uint8_t &operator[](uint16_t addr) const { return AT(addr); }

so, how to implement the function which returns const simply and clearly?

Netcan
  • 11
  • 2

0 Answers0