Sunday, 18 August 2013

Singleton, shared_ptr, raw pointer, or other pointer?

Singleton, shared_ptr, raw pointer, or other pointer?

class GameBoy{
public:
GameBoy()
:cart(new Cartridge()),
mmu(new MMU(cart)),
cpu(new CPU(mmu))
{}
void step(); //steps through one instruction
const CPU::State &getCPUState() const noexcept;
void loadROM(std::string fileName);
private:
std::shared_ptr<Cartridge> cart;
std::shared_ptr<MMU> mmu;
std::shared_ptr<CPU> cpu;
};
I am writing a Game Boy emulator and this class is what the Qt GUI will
interface with. That is, for each frame drawn in the GUI, it will call
GameBoy::step(). Obviously, it is not finished since I haven't started
work on the GPU.
I am currently questioning this design since these 3 classes (Cartridge,
CPU, MMU) will only be instantiated once. Would a singleton be a better
design than this, or would this current design be best?
If this one is best, should I stay with shared_ptr? I have read that it
should be used sparingly, but here, it seems to make sense since all 3
classes rely on each other.
Thank you very much!

No comments:

Post a Comment