Questions tagged [rule-of-three]

5 questions
20
votes
6 answers

The rule of 5 - to use it or not?

The rule of 3 (the rule of 5 in the new c++ standard) states : If you need to explicitly declare either the destructor, copy constructor or copy assignment operator yourself, you probably need to explicitly declare all three of them. But, on the…
BЈовић
  • 13,981
  • 8
  • 61
  • 81
17
votes
1 answer

Reasoning to wait until third time in the Rule of Three?

I just came across the article "Rule of Three" in wikipedia Rule of three is a code refactoring rule of thumb to decide when a replicated piece of code should be replaced by a new procedure. It states that the code can be copied once, but that…
Louis Rhys
  • 6,042
  • 11
  • 42
  • 59
9
votes
4 answers

If you follow the rule of Zero, how will you debug the construction of an object?

Everyone is quick to point out the "Rule of Zero" in code reviews, peer conversations, and Stack Overflow comments/answers. I am not a believer. I'd like to be. Usually if enough people agree on something enough for it to get its own label and…
Christopher Pisz
  • 437
  • 1
  • 5
  • 11
7
votes
3 answers

How to setup the c++ rule of three in a virtual base class

I am trying to create a pure virtual base class (or simulated pure virtual) my goal: User can't create instances of BaseClass. Derived classes have to implement default constructor, copy constructor, copy assignment operator and destructor. My…
Minion91
  • 173
  • 1
  • 5
5
votes
2 answers

Is the rule of 5 a valid extension of the rule of 3, or does it imply premature optimization?

I'm familiar with the notion in c++ of the rule of 3, however since the release of C++11 I've seen some sources suggesting it should be extended to a "rule of 5", I.e. the move constructor and move assignment operator should also be implemented…
Jules
  • 17,614
  • 2
  • 33
  • 63