Why does the Shard struct explicitly delete its copy constructor #64
Answered
by
Eamon2009
eamonsippy
asked this question in
Q&A
|
Why does the Shard struct explicitly delete its copy constructor (Shard(const Shard &) = delete;)? What catastrophic bug would occur if this was left as default and a Shard was copied into a std::vector? |
Answered by
Eamon2009
Jul 28, 2026
Replies: 1 comment
|
The Rule of 5 as in (#62)& The Shard Struct |
0 replies
Answer selected by
eamonsippy
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The Rule of 5 as in (#62)& The Shard Struct
The copy constructor is explicitly deleted (Shard(const Shard &) = delete;). If copying were allowed, you would have two Shard objects holding the exact same file handle (hFile / fd) or memory pointer (tokens). When the first object gets destroyed and runs its destructor (close_mmap), it closes the file and unmaps the memory. The second object is now holding dangling pointers, which will cause a catastrophic segmentation fault or double-free crash when it tries to read the data or destroy itself.