#include #include #include #include "println.hpp" struct StackObject { private: void* operator new(size_t size) noexcept { bool noStackObjectOnHeap = false; assert(noStackObjectOnHeap); return nullptr; } }; class HeapObject{ public: void* operator new (size_t size); HeapObject(); virtual ~HeapObject(); static bool assertionsHold(); protected: private: static int ctorCount; static int dtorCount; static int newCount; // static void remove(HeapObject *); HeapObject(const HeapObject&) = delete; HeapObject& operator=(const HeapObject&) = delete; }; int HeapObject::ctorCount = 0; int HeapObject::dtorCount = 0; int HeapObject::newCount = 0; void* HeapObject::operator new (size_t size){ newCount++; return new char[size]; } HeapObject::HeapObject(){ ctorCount++; } HeapObject::~HeapObject(){ dtorCount++; } bool HeapObject::assertionsHold(){ assert(ctorCount == newCount); // all objects have been allocated on heap assert(ctorCount == dtorCount); // all objects have been deleted return true; } typedef double MoneyUnitType; // should be fixed point class class Cash : public HeapObject { public: MoneyUnitType _value; Cash(MoneyUnitType value) : _value(value){} }; typedef std::vector Wallet; Wallet consumerWallet_A; Wallet consumerWallet_B; Cash * createCashFor(int i){ return new Cash(static_cast(i)); } void flushWallet(Wallet& toBeEmptied){ // should be in dtor of sorrounding class for(int i=0; i