C ++:三个规则,为什么?

时间:2012-11-16 12:25:44

标签: c++ oop

  

可能重复:
  What is The Rule of Three?

我在书中读到了当你用C ++设计类时的三条规则:

If you define any one of three in class, then you should define all of three.
1) destructor
2) copy constructor
3) Copy assignment constructor

我理解如何在C ++中实现它们。但是我无法解释自己为什么在我们已经拥有一个 ALL 时我们应该这样做。这之间的联系,请告诉我。

谢谢:)

1 个答案:

答案 0 :(得分:1)

基本上定义其中一个(并且不依赖于自动机制)的原因,同样的原因也适用于其他“构造函数”。