hey what's going on folks it's Mike here and Welcome to our next lesson in the modern C plus plus Series in this lesson we're going to be talking about something known as a funk door this is something that's been around in C plus plus for a long time and it's going to make sense to talk about it because we've actually got something that makes it a little bit easier to work with funk tours in modern C plus plus lambdas which we'll eventually get to in this series but with that said I want to go ahead and talk about what functors are they're actually really useful and they're nice building blocks for composing larger programs that said before we begin I just want to do a quick shout out to all of our members supporting the channel thank you for supporting this series and then all of our new subscribers welcome aboard so with that said let's go ahead and dive into this lesson all right so what I've got here is this idea of a funk door and you can kind of tell just from looking at the name it has something to do with a function and Tor I don't know how to explain that other than it sort of sounds like object or Constructor so we're constructing objects out of functions it's a function object and in fact that's what it stands for and basically what this means is that we're going to be able to create or sort of think about using an object like a function but as we know objects are things that have data and actions associated with them so in other words if I can clear this up just a little bit a functor is an object or rather a function with state and the key for holding this state or rather using an object like a function so that it can store its data is operator parentheses okay so we're going to go ahead and see how we use this to create a functor okay so let's go ahead and just start with the basics and I think this will make sense as we progress so what I'm going to go ahead and do is just create a struct and a struct is going to be a little bit easier to work with today and just create some sort of object here now the typical example is that folks use here are just going to be things like holding a number or value or something and that's what we're going to start with and then I'll show you something a little bit more pragmatic but what I'm going to go ahead and do here is just create some sort of value so here is some object here and let's just go ahead and have it hold some results here and in fact we'll probably want to initialize this to some value here I'll just initialize it to zero and let's hold on to that for now and then because we want to use this object like a function well we're going to overload operator parentheses here and then we can supply any parameters we want here so this is going to be the perhaps new result here okay and what we'll just do from this uh operator here is just return the new result and set our result equal to new results here okay now typically when I'm designing my classes too I also like to prefix my variables with M underscore so let's just go ahead and do that and we need a return type here for our operator and I'll go ahead and save this much and let me just do one quick little cleanup here make sure my parentheses are aligned and that I've got a semicolon here and let's go ahead and compile this so go ahead and compile this with C plus 20 but again just to show my point that this is something that's been around for a while let's go ahead and just do this in t plus 98 now if I do this in 98 it's going to complain about these curly braces here so I'm going to go ahead and get rid of those and compile this and again you can see that this is a feature that's been around for quite some time but let's go ahead and re-modernize our code here and make sure that we have everything there here okay so now how do I actually use this value object here well let's go ahead and create one here so I'm just going to call it value V here and again let's go ahead and now use this so because we've overloaded the parentheses here I can actually just call V and then the parentheses and then go ahead and pass in some value here like 42 here and let's just go ahead and print out V and we'll go ahead and do the result here now again I've just made everything public to make things relatively simple but if I go ahead and run this program and run it we get the value of 42. okay so this is where we have the sort of is a function with State here okay so what do I mean by function well just look how we're using this at line 16. I just have this function and I mean this looks like a function call here it's not a Constructor that's the other time where we've seen objects with parameters but it's just sort of standing alone and we're able to Pride uh some inputs for the arguments here in this case value 42. but what's interesting is that it's holding the state here it's actually holding the result here in one of the member variables this sort of data member here okay so that's where we get functor from okay so now you might be asking yourself why might we actually use this sort of functor well there's actually a couple reasons that we might want to do this let's say that we have perhaps a bunch of interesting results that we want to store in here okay so result and I'm just going to sort of label these generically two one and so on but what if for instance I'm only able to set one of these at a time perhaps I've got two processes running so what I can actually do is create another operator here and just to make this a little bit more interesting let's just make this a different data type here I'll make this a float here and if we happen to pass in a flow tier this will be for our results number two here's our result number two and let's go ahead and make sure we return that and we'll go ahead and return a float here okay now what's going on here is uh well you're saying Mike you've just overloaded operator and we know in C plus plus we haven't talked about it too much on this channel but we can overload these operators and this will call the float version setting result two this will call the inversion calling and setting result number one here and this doesn't seem like a big deal at first so let's go ahead and do it here now let's just go ahead and create this as 42.7 F and I'll be explicit so calls the right version and let's go ahead and print things out here and I'll do V Dot M result two and go ahead and recompile this oops made one little error here let's look see it is that I uh renamed these to result one so I gotta just go ahead and fix those so I'll do that at line 10 here and add line 24 and let's give that another recompile and I'll rerun it and my lack of spacing here let's go ahead and put a comma between these here just to show the two different results here and there we have it okay 42 and 42.7 so again you might be saying well what's the big deal here why were we able to you know compute these different results here this is sort of like just having a function that takes multiple parameters or even an object where I just have perhaps um you know two differential member function calls that set these member variables here and again you're completely right those would be other strategies that we could use but what if I have to start passing this function into or this object rather into multiple other functions or if I want to be able to compute different results on a different thread and sort of uh the point is here that I'm able to partially compute a result here and store the state here while this functor or this function object is Computing and storing the results here okay so it allows us to sort of break up work and sort of partially solve problems okay so this is a bigger idea in functors that I think is just kind of neat now with that said here I'm going to go ahead and just show you this example so you can see all the code here and then I want to go ahead and just show you another example that I think will get you to maybe buy in and see that funk doors are kind of neat here and the tool that they're going to allow us to build and use later in C plus plus Lambda functions unnamed functions is what's essentially built on top of a function so we gotta understand this first all right so here's the code here's what we did here with our struct here we did the operator overload here with parentheses and we computed two different results here for our bunker now again we could have done this in one function I could have made this two parameters but I wanted to show you how to break this problem up okay so with that said I want to go ahead and save this source code let's go ahead and make a second main here and go ahead and show you another example okay so let's just go ahead and prepare this so we'll get ready to compile main number two still the same example here but I'm going to go ahead and give you something maybe a little bit more realistic here so this time I'm going to go ahead and create a new struct here and I'm going to create a goblin here okay and let's just go ahead and give it an attribute like Health here so I can imagine some sort of game here where you're attacking some goblins now what I want to go ahead and do is initialize a vector of these goblins okay so I'll call this goblins and let's use a list initializer here again pulling out some of our modern C plus plus here to just construct a bunch of these goblins here okay so I'll have a goblin and I should provide a Constructor here and this will be good review for us in this series here let's go ahead and do a goblin here and provide uh Health here I'll just abbreviate it as H here and let me follow my convention by prefixing my variables with an M at the least sometimes an M underscore and we can take advantage of uh move assignment here when we construct this object here okay so that's our Goblin that we have created here or a Constructor for it and let's give them some health here this first goblin has five Health the next one's got three the next goblins got a hundred Health maybe that's a boss or something and so far so good so let's go ahead and compile this no mistakes okay so what do I want to actually do with these goblins well maybe and again you can imagine a game context you want to Target the weakest Goblin first and attack it so you can imagine some sort of system where we want to sort these goblins for whatever reason okay so let's just go ahead and using our algorithm Library we're going to use the sort function here okay so what I'm going to go ahead and do here is called begin on our goblins and end on our goblins which provides an iterator so pointing to the front of our Vector that's what this is doing and pointing to the end of our Vector that's what this is doing now we will talk about some STL in this series later on so you're going to have to subscribe to make sure that you don't miss that but let's just go ahead and leave this as is here I'll go ahead and compile here and hmm okay I'm getting a bunch of errors here so we got to kind of figure out what's going on here well first and foremost let me not jump too far ahead so we can either use CPP man which is a really nice plug-in and look for the standard sort function or for those of you who haven't seen my video on cpv man which I'm just going to go ahead and shut that down let's go ahead to our other favorite website and I'll bring in uh CPP reference here and let's just go ahead and look at standard uh sort okay and what sword's taking is well an iterator for the first and the last item which we have here okay we're taking in the first and the last thing but the issue is how are we sorting these goblins well we probably want to sort them by their health right that was our original goal so what I'm going to need to do is overload again using operator and then the less than operator and then I'm comparing my uh Goblin that's going to be essentially to the right the next one over so let's go ahead and say goblin and the right hand side and this is going to return a Boolean if this goblins uh Health here is less than the right hand sides uh Goblin's Health okay and healthier okay so let's go ahead and see if that helped us out here and looked like it did but we just need to return here true or false all right from that expression okay so now if I run this uh no result yet so let's go ahead and print this out and let me go ahead and just show you the code so you can see it on one line here and let's go ahead and break this down here just to make it a little bit nicer so you can see all of our goblins being initialized and then I can make this a little bit bigger and let's just go ahead and print these out here so uh let's just go ahead and while I'm introducing uh new things here let's go ahead and print out our goblins I'm just going to refer to them as G from our collection and again I'm throwing for those of you who've been watching this series uh consecutively some new things at you but this is a range based Loop which we've talked about in the series a while ago so I might need a refresher here and let's just go ahead and print off our goblins uh by their health here and we'll go ahead and put in Edline okay so if this is working if this sort function is working which needs some way to compare goblins and it's using their health and we have an operator less than so again what's going on in this uh sort function is it's looking from the start to the end and it's eventually it's essentially just comparing these using some sort of uh search or sort to algorithm rather and we have a way to tell what is or how is a goblin less than some other Goblin in this case the health attribute okay so let's go ahead and save this let's go ahead and compile see if we've made any mistakes no mistakes so we should see the Goblins sorted in this structure 3 5 and 100 and in fact we do okay so this works great here okay now what I'm going to go ahead and do is say well I'm making a more interesting game so again imagine this fictitional problem where I'm trying to Target the weakest Goblin okay I've already sorted them but but there's other properties that goblins have maybe their strength for instance so let's go ahead and add another attribute here strength and go ahead and say s here and we want to establish their strength here so let's go ahead in our initializer here and let's give them these two will be equal and let's go ahead and make this one very uh weak so it's got a lot of Health here but maybe we want to just you know take care of it um later to say or early it doesn't really matter but let's go ahead and compile this program again here and uh one mistake here so I've got a fix I've got to put a comma here because I'm initializing multiple uh member variables here and again it's still working but again I want to be able to sort by the strength now so I'm attacking the weakest Goblin first for whatever reason here okay so what we're going to go ahead and do here is hmm well I can't exactly use another operator here um because I have to choose here I've only got one Goblin that I'm comparing against essentially that's what I'm doing and you can kind of imagine this problem if you've ever done Excel or database programming where you might want to or even just looking through statistics say in some database where you're trying to sort by say two categories at once okay two attributes so in this case we might want to sort by uh the health or the strength or maybe even a combination of the two actually is a better way to do this so let's actually um just to make this a little bit more interesting what I'm going to go ahead and do is let's increase the strength of these ones by quite a bit here and I actually want to sort by the combination of their health and their strength here so what I could do here is again just add the health and the strength and maybe use that as a comparison but again maybe that algorithm is not going to work very well and we want to change things around so let me get to the point here and create a funk door which we've been learning about here and usually we call this sort of a special instance here I'm going to call this comparator for the Goblins so Goblin imperator and what I'm going to do here is I'm going to create a functor okay just like we did and why I'm doing this well when we look through a lot of the standard Library here again this was the sort function that we were able to call but we again want to be able to use some sort of third argument that can do a test here to help sort us and that test here is basically a comparison is the left hand side or the right hand side greater or less okay and you can see that we have this sort of compare uh overloaded version of sort here so this third attribute here that we can Implement and that's what we're doing here with Goblin comparator okay so let's go ahead and we're going to return some Boolean here and because this is a funk door again the operator that we are overloading is the uh parentheses here and we're going to take in a goblin again I'm going to make these cons because they're not changing pass them by reference and we're comparing the left hand side with the Goblin on the right hand side and again there's some things that we might want to do here just to you know make our code uh nice here uh you know we could make this uh const for instance um just to be uh you know none of the member variables are changing in this particular comparator and let's go ahead and now do our comparison and again it's got to return true or false here so I'm going to say the left hand side uh and let's actually just use the strength attribute now I'll just use one of these um less than the right hand side uh strength here okay and let's go ahead and just return this and it's just a true or a false value here and again uh this time for my sort function I want to do a different test so I'm just going to pass in my Goblin comparator here okay so let's go ahead and see if this works if it's as simple as that so I'm going to go ahead and compile let's see it looks like I'm missing a semicolon at line 18. I thought I'd get it perfect but there's always one little thing here but it does in fact compile and it runs here okay and now we can see how our goblins are being sorted not by their health here but essentially in reverse order so this one here with the lowest strength uh it but the highest health is first and then the next one and then the next one and the next one okay so we can kind of see how I could use different comparators or this idea here of different tests here as parameters into functions that change our Behavior meaning how we are changing how we do this sorting function now what we're going to find is there's actually a lot of different algorithms here which is what sort is part of and I've omitted this uh explicitly calling out but they're part of the algorithm Library which is going to be a really important part of the standard template library and that's where we're going to be moving in a little bit after we talk about some more of the containers in this series so I hope that was useful I hope that sort of motivates funktors these functions with state or function objects if you will they're really useful in the sense that we can use them as building blocks for solving larger problems whether you want to think about the previous example which I'll bring up here just as we recap here in our main where we can sort of compose or partially solve some problem if we want for a larger object or just simply for convenience have a function that we can call and record States a lot of times folks will also use these just to have counters for how often functions are called or writing little test functions and then the other case which I find myself more often using for Funk doors so let me go ahead and open up what we just solved here is when you want to do some sort of comparison or a test here so you can imagine again some object with multiple attributes and you want to write some way to compare them or evaluate them well you can just have several different overloads of this operator parens here okay folks so with that said I hope this was an interesting lesson that opened your eyes into Funk doors they sound kind of scary when you first learn about them but this should give you a nice introduction to what they actually are and how to use these Funk tours so with that said hope you enjoyed this lesson hope you learned something about Funk doors if you have comments even if you're just not happy about goblins with the least strength and the most health being attacked first go ahead and let me know like you discussed that in the comments and I'll look forward to that all right with that said folks thanks for your time and we'll talk to you soon see you in the next lesson
Get free YouTube transcripts with timestamps, translation, and download options.
Transcript content is sourced from YouTube's auto-generated captions or AI transcription. All video content belongs to the original creators. Terms of Service · DMCA Contact