128: Data Types: Function Objects Part 1.

Take Up Code - Un pódcast de Take Up Code: build your own computer games, apps, and robotics with podcasts and live classes

Categorías:

Function objects are simple but don’t let that fool you. You can use them in clever solutions. It’s more difficult to explain why you might want to use a function object if you don’t yet know what a function object is. So that’s mostly what this episode explains. What is a function object? They’re sometimes called functors for short. In many ways, you can think of a functor as a more complicated way to write a method. That’s what I hope to explain because there are some unique benefits to writing a method in the form of a function object. Alright, a method has a signature. Let’s say you have a method that can add two integers. How would you write this as a functor?class add { public: add() {} int operator () (int first, int second) { return first + second; } };The big question now is why? What possible benefit could come from such a roundabout and complicated way of writing a method? Well, if this is all the functor was going to do, then I’d agree. You should just use a method. But you have a class now and that means it can have other class member variables that control it’s behavior. The only way to change the behavior of a method is to pass in different arguments. But with a functor, you can construct the class with whatever information you want and even call other methods that can change or modify internal data members. Then when you want to use it as if it was a function, it can refer to its own internal data to do different things. Listen to the full episode for more or read the full transcript below. Transcript You already know how to write a function. A lot of times, probably most of the time actually, I call functions methods. It’s the same thing at least in C++ and C#. Some languages might have differences between functions and methods such as one may be allowed to return values while the other can’t. I tend to use the two terms as if they’re the same thing. The topic of today’s episode though is never called method objects. At least, I’ve never heard them called this. They’re always function objects or sometimes functors for short. It’s more difficult to explain why you might want to use a function object if you don’t yet know what a function object is. So that’s where I’ll start today. What is a function object? In many ways, you can think of a functor as a more complicated way to write a method. That’s what I hope to explain because there are some unique benefits to writing a method in the form of a function object. Alright, a method has a signature. Let’s say you have a method that can add two integers. I know, you can just add them directly without needing to call a method but bear with me for a moment and think about a method that can add two integers. It should return an integer, have a good name, we’ll call it “add”, hey, sometimes the simple names are best, and the method should take two arguments for the integers to be added. Once you have the method declared, you implement it by coding the body of the method. This is where you add the two argument values and return the result. Once the method is declared and implemented, you can call it from your code by first declaring an integer variable to hold the result. That’ll be called sum. Then you just write sum is assigned add open parenthesis, give the two numbers to be added, and then a closing parenthesis. The key to calling a method is to use the method name, the open and close parenthesis, and the proper number and type of arguments expected by the method. The return value if any is up to you to decide what to do with. Those parenthesis characters though are critical. They’re the key to letting the compiler know that you want to call the method using the supplied arguments. I’ll explain how to do the same thing with a functor right after this message from our sponsor. A functor needs a class

Visit the podcast's native language site