gpt4 book ai didi

c++ - 您如何声明和使用重载的池运算符删除?

转载 作者:可可西里 更新时间:2023-11-01 17:58:03 33 4
gpt4 key购买 nike

我想知道如何适配section 11.14 of the C++-FAQ-lite到数组。

基本上,我想要这样的东西:

class Pool {
public:
void* allocate(size_t size) {...}
void deallocate(void* p, size_t size) {...}
};

void* operator new[](size_t size, Pool& pool) { return pool.allocate(size); }
void operator delete[](void* p, size_t size, Pool& pool) { pool.deallocate(p, size); }

struct Foo {...};

int main() {
Pool pool;

Foo* manyFoos = new (pool) Foo [15];

/* ... */

delete [] (pool) manyFoos;
}

但是,我一直无法弄清楚声明和调用此 operator delete[] (pool) 的正确语法。有人可以帮忙吗?

最佳答案

首先在单个对象上调用 dtor,然后使用:

for (int i = 0; i < 15; ++i) manyFoos[ i ]->~Foo();
operator delete[] (manyFoos, pool);

您可以再次阅读整个常见问题解答项目,您会在那里找到它。

关于c++ - 您如何声明和使用重载的池运算符删除?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2322742/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com