Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
给定一个已分配但未初始化的内存位置,我如何将一些对象移动到该位置(破坏原始位置),而不构造可能昂贵的中间对象?
您可以使用placement new 在内存中移动构造它:
void * memory = get_some_memory(); Thing * new_thing = new (memory) Thing(std::move(old_thing));
如果它有一个重要的析构函数,那么你需要在完成后显式地销毁它:
new_thing->~Thing();