Is self-initialization 'A a = a;' allowed?

前端 未结 4 658
半阙折子戏
半阙折子戏 2020-12-01 21:46

This code fails at runtime in the copy constructor.
But the compiler (MSVS2008) issues no warnings.

Could you explain (preferably cite the standard) whether this

4条回答
  •  甜味超标
    2020-12-01 22:28

    A a = a; definitely should not be written. But a = a could be written. Your assignment operator must check for &rv == this and do nothing in case of a self-copy.

    Oh, yes, you do need to write an assignment operator for class A.

提交回复
热议问题