129: Data Types: Function Objects Part 2.
Take Up Code - A podcast by Take Up Code: build your own computer games, apps, and robotics with podcasts and live classes
Categorie:
Function objects are simple but don’t let that fool you. You can use them in clever solutions. How do you actually use functors? A really great example is in the C++ standard library collection classes. Some of the collection classes support sorting. In fact, some of them require sorting. Now you might think, okay, what’s so special about that? And if all you have is a collection of numeric types or maybe a collection of dates, then, yes, sorting is fairly easy. But wait a moment. Which way? Should your collection hold 1, 2, 3, or should it hold 3, 2, 1? Even numeric values have a little extra functionality. What about custom classes with several member data fields? Should they all be looked at? In what order? Or do you let the user change the sort order by clicking column headers? What if you want to sort by one property and then another? Let’s say you have a list of items for sale. Maybe the user wants to see them sorted by price. That’s good but what if a whole bunch of them all cost the same? Should similar priced items then be shown in a random order? Probably not. It makes sense to sort the items by price since that’s what the user selected but then sort them again by name for items of the same price. You can write your own function object that knows exactly how to work with whatever properties you need. But using functors, gives you more benefits than just this. Listen to the full episode for more on functors, or you can also read the full transcript below. Transcript This episode continues the explanation of function objects. Now that you know what a functor is and how to write one, I’ll explain more about how to use them. I mentioned that because a functor is an instance of a type that you can pass it around and make use of instance data to customize its behavior. But you could, with a bit extra work, get a function pointer to do something similar. Not quite, but almost. There’s more though than just this to a functor. Because its an instance of a type, we can use that type in templates. A method has a signature but a class has both a type and potentially internal data. That actually sounds like just more theory. How do you actually use functors? A really great example is in the C++ standard library collection classes. Some of the collection classes support sorting. In fact, some of them require sorting. Now you might think, okay, what’s so special about that? And if all you have is a collection of numeric types or maybe a collection of dates, then, yes, sorting is fairly easy. But wait a moment. Which way? Should your collection hold 1, 2, 3, or should it hold 3, 2, 1? Even numeric values have a little extra functionality. What about custom classes with several member data fields? Should they all be looked at? In what order? Or do you let the user change the sort order by clicking column headers? What if you want to sort by one property and then another? Let’s say you have a list of items for sale. Maybe the user wants to see them sorted by price. That’s good but what if a whole bunch of them all cost the same? Should similar priced items then be shown in a random order? Probably not. It makes sense to sort the items by price since that’s what the user selected but then sort them again by name for items of the same price. Well, the container classes have absolutely no idea how to do all this. I’ll explain how functors can help right after this message from our sponsor. One of the template parameters that is sometimes available depending on the collection is a sort operation. Let’s say you want to create a set to hold integers. If you just write set<int>, then you get the default sort operator less. What is less though? I’m not talking about the concept of what it means for one thing to be less than another. I’m talking about the specific default less that you get when you don’t specify anything else when creating a