QA Friday 2016-Mar-04

Take Up Code - A podcast by Take Up Code: build your own computer games, apps, and robotics with podcasts and live classes

Categories:

What is the rule of three? And related to this: What is the rule of five? And what is the rule of zero? When you need to implement either a destructor, a copy constructor, of an assignment operator, then the rule of three says that you really need to implement all three of these methods. If you don’t, then the compiler will provide a default implementation for you. And if you’re declaring and implementing a destructor, for example, because you need to clean-up some resources, then you’re almost certainly going to want to manage those resources when you make a copy of your object and when you assign one object instance to another. The rule of five is a more modern version of the rule of three and it adds two more methods that should be implemented together with the other three. This involve move semantics and the methods are the move copy constructor and the move assignment operator. The rule of zero is really just the same thing stated the other way around. This says that when you don’t need these methods, then don’t declare any of them. But what if you have a class that otherwise would not need these methods but because it’s intended to serve as a base class, then it needs a virtual destructor? In this case, you’re adding a destructor not for managing resources, but just to enable polymorphism to work properly through the base class. When you have to declare a destructor like this, then the rule of zero says that you should go ahead and declare the other four methods, the copy constructor, the move copy constructor, the assignment operator, and the move assignment operator, and specify that they should all be default. This will signal the compiler to provide them for you as if you had not declared any of them. Listen to this QA episode for more or you can read the full transcript below. Transcript Thank you for your review and comments. If you’d like me to read your review, well, the first step is you have to leave a review. The thing I liked about this review was the understanding. It’s a big goal for each episode to relate to something that you can understand. Okay on to the question this week. Very few things are actually rules and normally when I think of rules in C++, the first thing that comes to mind are the compiler syntax rules and linker rules. If you get these wrong, your program won’t build. Either it won’t compile or it won’t link into the final product. The rule of three is different because the compiler usually won’t warn you when you forget. And your program will almost certainly not work properly. You already know that you can declare classes and structs with member methods. But did you know that the compiler will write some methods for you if you leave them out? This is not a bad thing and many times, the compiler generated methods will do what you need. You normally run into problems when you try to provide your own implementation for some of these methods. Let me give you an example. Let’s say you have a robot that knows how to bake a cake. And you were very careful with this robot to make sure that it never forgets to turn the oven off when it’s done. The moment it takes the cake out of the oven, it pushes the bake button. Now some ovens have a special off button but your oven designers thought that was a waste of buttons and decided to have a single button that when pressed turns the oven on and then when pressed again turns the oven off. Your robot is quite the success and you decide to build another one. After all, you should be able to bake two cakes at the same time and another robot would allow you to manage the baking in parallel. That just means that each robot is responsible for its own cake and they’re both in the kitchen at the same time. The problem here is that they’re sharing the same oven. This is fine for the actual baking of the first cake. But what about the second one? Let’s say