Can I make a bitwise copy of a C++ object?

后端 未结 6 2166
误落风尘
误落风尘 2021-02-09 18:52

Can C++ objects be copied using bitwise copy? I mean using memcopy_s? Is there a scenario in which that can go wrong?

6条回答
  •  终归单人心
    2021-02-09 19:46

    In addition to the problem of unbalanced resource management calls in the two instance you end up with after a memcopy (as @JaredPar and @rmeador pointed), if the object supports a notion of an instance ID doing a memcopy will leave you with two instances with the same ID. This can lead to all sorts of "interesting" problems to hunt later on, especially if your objects are mapped to a database.

提交回复
热议问题