How to actually implement the rule of five?

后端 未结 6 1139
遥遥无期
遥遥无期 2020-11-28 04:05

UPDATE at the bottom

q1: How would you implement the rule of five for a class that manages rather heavy resources, but of which you

6条回答
  •  借酒劲吻你
    2020-11-28 04:29

    Since I haven't seen anyone else explicitly point this out...

    Your copy assignment operator taking its argument by value is an important optimization opportunity if (and only if) it's passed an rvalue, due to copy elision. But in a class with an assignment operator that explicitly only takes rvalues (i.e., one with a move assignment operator), this is a nonsensical scenario. So, modulo the memory leaks that have already been pointed out in other answers, I'd say your class is already ideal if you simply change the copy assignment operator to take its argument by const reference.

提交回复
热议问题