We are doing objects and references in the First Year tutorial today. Great fun. Well, at least I thought so. I asked the class whether big objects in memory have larger references than small ones. They don’t – the size of the tag is always the same – no matter what it is connected to. I went on to explain that a reference tag contains a bunch of information about the thing it is referring to, including the type that it has, for example string, BankAccount, AlienSprite or whatever class you have created.
Then someone asked a great question: “What happens if the type has a very long name? Does the reference tag get bigger?”. Aha! What a great question. The answer is no. This is because the type of a reference is managed in terms of a reference from the tag to the type object that describes that reference. In other words, a reference to a BankAccount will also contain a reference to an object that describes the BankAccount class.
I was very pleased with this question, because it let me start to explain how, by using objects, you can build up structures of data that are genuinely useful. I’ve been explaining structures and objects all week and several times I’ve had the sensible question “What’s the point of objects and references? They just seem to make life harder for us.” This little allows me to show how easy it is to use references to allow the system running the program to track and mange the type of the objects it is using.
I reckon that a good tutorial is when the students learn something. A great tutorial is when the tutor learns something as well. I’ve now got a lovely new example to use next year…