MyObject

 avatar
unknown
c_cpp
5 days ago
932 B
4
Indexable
#include <iosfwd>
#include <string>
#include "pool.h"

using std::string;
using std::ostream;

class MyObject {
    int id;
    string name;
    inline static Pool<MyObject> pool;

    // Disallow copy, assign, and direct construction
    MyObject(const MyObject&) = delete;
    MyObject& operator=(const MyObject&) = delete;
    MyObject(int i, const string& nm) : name(nm), id(i) {}

    static void* operator new(size_t) {
        return pool.allocate();
    }

public:
    static void operator delete(void* p) {
        pool.deallocate(p);
    }

    static MyObject* create(int id, const string& name) {
        return new MyObject(id, name);  // This will use custom `new` operator
    }

    static void profile() {
        pool.profile();
    }

    friend std::ostream& operator<<(std::ostream& os, const MyObject& o) {
        return os << '{' << o.id << ',' << o.name << '}';
    }
};
Leave a Comment