18: Constructors.
Take Up Code - A podcast by Take Up Code: build your own computer games, apps, and robotics with podcasts and live classes
Categorie:
Constructors are your first opportunity to make sure your custom types are well formed. This episode explains the different kinds of constructors and how you should use them. The first thing to understand is that constructors are just methods in your class that have some special rules. And you don’t normally call constructors directly. Your classes can have multiple constructor methods depending on different uses or how easy you want to make it for other code to create instances. One special constructor is called the default constructor. You’ll use this when you want to be able to construct instances without needing to specify any additional information. This is really just a constructor that takes no parameters. There can only be one default constructor per class. Another special constructor is called the copy constructor. There can also only be a single copy constructor per class. This is used whenever you want to create a new instance that is a copy of some other variable of the same type. You write this method to take a single parameter of the same type. Usually this will be a constant reference to the other instance. Other constructors will be whatever you need and will have different parameters. Listen to the full episode or read the full transcript below for more. Transcript Let me start by saying that constructors are just methods that have some special rules. These rules vary from one language to another and because I don’t know all languages, some of this may not apply to your language. These fundamental concepts I’m going to describe should help you to better understand constructors in any language. The first rule is that you can’t just call a constructor anytime you want like other methods. The previous episode introduced classes as types. While you can work with a type directly, you’ll almost always be working with instances of types. A constructor helps you create these instances. When you declare a variable such as a float, you just provide the type, which is float, a name that you choose, and optionally, an initial value. You now have an instance of that float. That means you have some real memory dedicated to just this one float that’s identified by whatever name you gave to your variable. But what if you wanted a special kind of float that could never have negative values? You’ll need your own class for this special purpose float. And you’ll want to write methods that handle the initial construction of instances of your class to make sure they’re protected from the very beginning. Or maybe you have a more complicated example in mind. Maybe you need a class to represent a character in an adventure game. This class will need lots of data of different types and all this data will need to be coordinated to make sure it all makes sense together. You’re going to want your character instances to be valid from the very beginning. The alternative is to allow a new instance of your class to be created that cannot or should not be used until certain properties have been set. In other words, certain data needs to be set first. What that is is up to you. Do you really want to have to call a series of methods to set this information? Or do you want to make a separate call to some initialization method? What if you’re creating a whole bunch of these classes? You may not always be able to properly initialize each one. This is the job of constructors. Your classes can have multiple constructor methods depending on different uses or how easy you want to make it for other code to create instances. So what does a constructor look like? Most languages require constructors to have no return type. I’m not talking about a void return type here. This is an exception to the rule that all methods need a return type. The reason is because you don’t really call constructors like normal methods. The compiler will call the constructor for you whenever you