3

I have been reading C++ and writing small programs in it for more than a year. Recently I came across Law of The Big Three. I never knew about this law.

Accidentally, I found it here: Rule of Three.

May I know any other such laws in C++?

Ben
  • 51,770
  • 36
  • 127
  • 149
emeh
  • 823
  • 1
  • 7
  • 21
  • Have a look into the books listed at . Many of them cover such topics. The C++ FAQ at http://www.parashift.com/c++-faq-lite/ is also very helpful. – gimpf Oct 02 '09 at 12:09
  • Don't take this "Law" too seriously. The default copy constructor is fine for most cases, and you only need the assignment operator if you're using your classes in a fancier way than most classes are used. – Kieveli Oct 02 '09 at 12:15
  • 5
    @Kieveli: the law says *if* you define one, you should define all three. I think it should be taken fairly seriously: other than logging there's not much of significance that you can do in any of those three functions, which doesn't imply that you need to do corresponding things in the others. Note that the law does not say that you should define them for all classes, it just means that if for instance you free resources in the destructor, then the default copy and assignment need to be suppressed or replaced. – Steve Jessop Oct 02 '09 at 12:22
  • @Kieveli: I second onebyone here, if you actually have to define one of those 3 it means your class is special and you leave yourself open to bugs if you don't define the other 2. – Matthieu M. Oct 02 '09 at 13:15
  • C++11 expands this to a rule of 5. Or 3, 4 or 5. http://stackoverflow.com/questions/4782757/rule-of-three-becomes-rule-of-five-with-c11 – Phil H Feb 13 '13 at 13:18

4 Answers4

14

You're probably looking for C++ "best practices", not "laws". This should help you searching on the net.

Moreover, there's a book called "C++ Coding Standards: 101 Rules, Guidelines, and Best Practices" by Herb Sutter and Andrei Alexandrescu which is supposed to be good, but I haven't read it myself. You can order it, e.g., over at amazon.com.

middus
  • 9,103
  • 1
  • 31
  • 33
7

For this kind of thing I recommend:

C++ Coding Standards: 101 Rules, Guidelines, and Best Practices

Daniel Earwicker
  • 114,894
  • 38
  • 205
  • 284
5

I also find Scott Myers' "Effective C++" series very useful, written in a very readable and memorable style.

Polyfun
  • 9,479
  • 4
  • 31
  • 39
1

Well, it seems everyone agree on the value of Herb Sutter and Andrei Alexandrescu's amazing book.

C++ Coding Standards is a most interesting read. The items are grouped very logically, and each item is detailed just enough that you understand the risk for not heeding the advice. Also each item comes with possible exception to the rule and why they are exception.

All in all it is a very valuable asset to one trying to put in place a number of practices, especially when installing code reviews.

There is another 'book' though only available in PDF / HTML format as far as I know. It is a mash-up of several standards referenced

High Integrity C++ is also very interesting in that it qualifies the items (Rule / Guideline distinction) and sometimes offer alternatives (two mutually exclusive advices). It is much more detailed than C++ coding standards so you might want to read it in second position :)

You can obtain the PDF version here, they will send it to you by email.

Matthieu M.
  • 287,565
  • 48
  • 449
  • 722