By way of explanation, I'll focus on just the issue of manual memory management. Simplifying a bit, consider this part of the C64 class's implementation:
C64::C64()
{
cpu_ = new Cpu();
mem_ = new Memory();
cia1_ = new Cia1();
...
cpu_->memory(mem_);
cpu_->reset();
...
cia1_->cpu(cpu_);
...
mem_->cia1(cia1_);
...
}
C64::~C64()
{
delete cpu_;
delete mem_;
delete cia1_;
...
}
The contract in this code is that a C64 owns the chips, the chips are born when the C64 is instantiated, and when the C64 dies, the chips all die along with it. There are two approaches for making this contract explicit, one is to simply not use pointers and references at all: Cpu cpu_;
Memory memory_;
Cia1 cia1_;
But this does mean we can't use polymorphism within the C64 class, we don't have nullability, and we can't replace the instances. The code doesn't leverage any of these, so that may be an appropriate choice. However, if we need any of those things, we can instead use the appropriate smart pointer: std::unique_ptr<Cpu> cpu_;
std::unique_ptr<Memory> memory_;
std::unique_ptr<Cia1> cia1_;
In either case, C64's destructor is no longer necessary. The other classes still take raw pointers (e.g., `Cpu*`) because they do not own the objects they receive and do not dictate lifetimes.It's worth mentioning that many people would use `std::shared_ptr` here instead, but I think that's abuse that leads you towards circular references (memory leaks): the Cpu doesn't own its Memory, they just have their lifetimes managed exterior to them. This would actually come up with the existing design (if shared ownership were used), since the object dependency graph (which is perhaps another issue) has several circular dependencies.