Function pointers, delegates and callbacks | Beginner friendly

CodeBeauty4,229 words

Full Transcript

hi guys and welcome to my channel in this video I'm going to teach you about callbacks and delegates you will see this callback delegate pattern very often and I know it's very confusing for beginners it was confusing for me when I first saw it so in this video we are going to learn everything that you need to know about it and I will also teach you about function pointers I know that's a very hard topic for beginners and I have a separate video where I talk about it in depth where I talk about syntax and all so make sure to check that video it will be linked here and also in the description uh because in this video I'm not going to focus as much on the syntax but definitely after watching this video you will understand what our callbacks delegates as well as function pointers and when you should use them right away if you need additional resources to learn programming with great explanations and examples I am going to leave a link down below and also in today's video we are using C plus plus Builder because it has very good visual way that will help you to understand today's topic better so uh if you want to download it I will also put a link in the description uh and I would recommend personally to watch the video once so that you can get a bigger picture and then you can code along with me and watch the video a second time so the first question is what are delegates delegates give you the possibility to pass methods as arguments to other methods in some languages like c-sharp for example you have this idea of delegate but in the background they are implemented with the help of function pointers now in languages like C plus you know that you have the option to work with function pointers directly and that is exactly what we are going to do in order to implement this delegate callback pattern so that once you understand how it works in the background and once you understand the idea and the logic in C plus it is going to be extremely easy to apply to any other programming language regardless if that language has slightly different syntax or different naming for these Concepts and things like that another question is are callbacks the same as delegates and the answer is no they're not the same but they are connected let's say that they are two sides of the same coin and the difference is the perspective that you look from and you will understand this you will have it clear once we implement the example that I want to show you and once you see the code so let me show you the application that we are going to build so here is the application that we are going to build it consists of two forms that communicate with each other so you can send messages back and forth from one form to the other and they are in the parent child relationship so let's send a message from the first one let's say hello my child okay and click Send and as you can see here you will get that message and here you can reply you can say hello my parent s okay and then send message back and you get that message here and then again let's say how are you my child okay send message you get that message on your child component and let's say good how are you my parent okay and then you get that message back so you get the idea of the application that we are going to build so now let's see how we are going to build this application and how we are going to establish the communication between these two forms so you already saw the application that we are going to build and the tool that we will use for that is C plus Builder I will put a link in the description so that you can download it so let's create a new application so a new multi-dice application with C plus plus Builder and then click blank application okay so our application will consist of two forms that look almost identical so the first step is going to be to create those two forms and build user interface so here I have unit 2.cpp we will rename that so that it is called let's say parent that is going to be our parent form so here we have parent.cpp so our CPP file and then parent.fmx that is design file and then parent.h that is header file where we put all of the Declarations of functions so that is our parent form and then let's add another form I will right click on project and click add new Multi-Device form okay and I am going to rename this form as well so rename and let's call it child like this so by default for each one of these that you created you get an empty form as part of your design so if I open my child.fmx here is my form it is empty and what I want to do is I just want to rename it so that it is easier to remember its name and work with this form so I will find its name property and I will call it let's say child form okay and then I will go to my parent Dot fmx and here I will change the name so that it is called parent form okay so what I want to do now is I want to put all of the necessary components and elements to our form and we need three elements we need edit so we need an input field let's make it a little bit bigger and then we need button so t button let's also make it a little bit bigger and then we need a label so T label and I will put it here and also make it a little bit bigger and adjust the size of the form so now let's change the name of these controls so I will click on my edit and I will change its name so that it is called parent input field okay and then I will change the name of this button I will call it send message to child button okay and then let's change the name of the label and let's call it message label okay and then I will do the same for my child form so click on child.fmx and add all of the necessary controls so edit I'll put it here and then button I will put it here and then I also need a label that goes at the bottom and then adjust the size of my form and again I will change the name so this edit will be called let's call it child input field okay and then this button will be called send message to parent button okay and then the name for this label is going to be let's just say message label so we have built user interface for both of the forms that we need for our program the next step is to establish the communication between parent form and child form so that you can pass a message from the parent to the child and there are a few ways to do this since the parent form will be instantiating the child form this means that the parent will have access to the properties of the child form that is one way another way is again because parent will be instantiating a child it can pass parameters to the Constructor of the child and that is a project we will use in this video so what I want to happen is following make sure to position yourself on parent.fmx so once the user types his message in this input field and then clicks on this button what I want to do is I want to pass that message to my child form okay so let's double click on this button in order to create an event and inside this event we will describe behavior that I just explained so inside this function here we will create a new instance of child form and inside its Constructor we will pass a message that our user has typed so let's very quickly check what is the name of this type so this is the name of the type and the name of the Constructor and the Constructor receives one parameter for now we are just going to treat this as not important and then later I'm going to explain what is this used for so let's copy the name of this type and go back to our parent.cpp and here I want to create a new child form so I will say t child form and the name will be child form so I'll say please create a new T child form and as you saw the Constructor receives one parameter for now I will just say that I will pass now PTR again later at the end of this video I will explain what that parameter represents so for now just pass null PTR okay and another thing is since we are using new keyword here that means that we are working with pointers so you have to indicate that here by putting this star symbol so this says that you are working with pointers okay so with this we have created a new instance of our child form and what I want to do is I want to pass additional message to that child for it so that message is going to be the text from this input field and the name of this input field is parent input field you can check it inside this object inspector so I'll go back to my parents.cpp and here I will pass parent input field it's text and make sure to make this capital letter T like this okay so here we have passed the text what I need to do now is I need to receive that text in the Constructor of my child.cpp so here I'm going to change the Constructor to receive a parameter and that parameter is of type and C string so I'll say a-n-s-i string and I will call it message okay so what I want to happen is once I receive this message I just want to show that message inside this message label so I will do that here in my child.cpp I will say message label it's text will be equal to this message okay and then again please make sure that you use capital letter T here or else you will get an error okay now let's go back to my parent.cpp so once I have created a new instance of my child Forum what I want to do with that form is I just want to show it so I will say child form show like this so that is how you pass a message from the parent to the child now one important thing before we run this program because right now it's not going to work and it doesn't have anything to do with delegates or callbacks it's just the way that C plus plus handles things that is the following if you watch my video about header files you know that inside the header file we put all the Declarations of of functions and right now we have changed the Declaration of this child Constructor okay we have added an additional parameter so what we need to do is we need to change that inside child.h so inside child header file as well because here here we have previous Constructor so I'm just going to change that here like this make sure that you don't forget this semicolon sign if you accidentally delete it okay so if I run my program now let's see if everything is going to work as we expect okay we have an error and that error says unknown type name t child form so the reason why we are getting this error is following go back to your parent.cpp and here you will notice that we are using T child form now this t-child form type has been declared inside child.h header file so the same way that you are including here parent.h you also need to include child.h okay and with this we should fix these two errors that we have okay so if I run the program as you can see we get our parent form so let's pass a message to our child let's say hello my child like this and then send this message and as you can see we get our child form and here is the message hello my child so what I want to do next is I want to be able to return a message back to my parents so I want to be able to see something like hello my parent okay and then I want to be able to send this message and then show it here so let's see how we are going to implement that let's close our program take a look at this line of code here where we have created a child form and to its Constructor we have passed a text parameter so the same way that you can pass a text parameter you can also pass a function pointer and that is exactly the approach that we are going to use we are going to create a function on our parent.cpp and the job of that function will be to show messages on our parent form and then we are going to pass a pointer to that function to our child form so that when the user types a message on the child form and presses the button we are going to invoke that function pointer which means to invoke that function that will show the message on our parent form so let's see how this works in practice how we are going to type this in code so the first step is to create a function that shows messages on our parent form so I will do that here and that function is going to be of return type void and then I will say that it belongs to T parent form like this this is just a namespace and I will call this function show message on parent okay and this function should receive a message as a parameter so it will receive a parameter of type NC string so a n s i string and I will call it message okay and again the job of this function will be very easy so it will just show the message inside this label so the name of the label is message label and I will just say message label it's text property will be equal to this message here okay so that is our function now step number two is to create a function pointer that will point to this function here so that we can pass it to our child form so we are going to create a function pointer here inside our button click method and the syntax that you use to create function pointers is kind of complex for most people so we will go through it step by step so I want to create a function pointer to this function here so that means that I want to create a function pointer that points to any function that returns void type and it receives one parameter of type NC string so let's create that pointer the syntax goes like this so first you specify the return type and the return type is void the same as the return type of the function okay and then you give it a name inside parentheses you will specify that you are talking about pointers like this and then you will give it a name so let's call call it funk PTR like this and then inside additional parentheses you will specify perimeter list and here we have only one parameter of type NC string okay so with this we have created a function pointer that points to any function that returns void and receives one NC string parameter and the name of that function pointer is func PTR so what I can do now is I can say that this func PTR points to this function here like this and one important thing is that whenever we are whenever you are working with method pointers you need to add here underscore underscore closure like this now I can make an additional video where I would explain what each one of these keywords means but without going into too much detail just remember that when you're working with method pointers you need this closure keyword in order to not get errors because if you forget it you are going to get an error so we have created a function pointer and we have assigned it a value so now it is pointing to our show message on parent function so what I can do now is I can pass this function pointer to my child okay so the next step is to go into child.cpp and then receive this additional parameter inside our Constructor so I'll just copy this entire type of the function pointer it's declaration and I will go into my child.cpp and here inside the Constructor I will paste it like this okay so what I want to do now is I want to store this function pointer that I have received in my child form and for that I will need to create a function pointer here on a global level so I'll just paste the same declaration and I will change the name here I will call it show message on parent callback okay and don't forget semicolon at the end so here I have declared a function pointer and I just want to assign it the value that I have received in my Constructor like this okay so now what I can do on my child form is I can invoke this function pointer which means invoke this function and pass it a message and that message is going to be shown on my parent form so what are we going to do that well we are going to do that when the user clicks on this button so let's double click on the button in order to create an event okay so here is our event and the one that event is triggered I want to invoke this function I want to translate the text from my input field on this child.fmx let's check the name of the input field so the name is child input field let's go back to child.cpp and here to my function pointer I will just pass the text from that field like this and after I have passed that text let's just say this close okay make sure to use Capital C here okay and this command is just going to close our child form so that is the entire logic of how delegate callback pattern Works let's go through it one more time so on my parent.cpp I created a function that shows messages on parent and then I created a pointer to that function here so I have passed that pointer to my child form and then I have received it here in the Constructor of my child form and I have stored it in a global variable so Global function pointer variable I've done that here so that I can invoke it from different functions and then in this button click event when the user types a message and clicks send I have invoked that function with this command here and then I've just closed the form Okay so that is how delegate callback works but we will need a couple more things that are not related to delegate callback pattern they're just related to the way that c plus works so let's go back to parent.cpp and let's see a couple of things first here we have added a new function it's called show message on parent so as you already know every function declaration should go to header file so I am going to copy this declaration and then go to my parents.h and here I will say hey you have a function that is called show message on parent and that function receives one parameter and then let's add semicolon here okay so that is what I should do inside parent.h and then on my child.cpp since I have again changed the signature of this Constructor so now it receives one additional parameter I also have to update that inside my child.h so inside the header file here I will change parameter list of the Constructor and again please make sure to add semicolon at the end okay so after you do that let's run our program and let's see if everything will work as we expected as you can see here is our parent form so I'll say hello my child and then send the message we get that message on our child Forum so let's now say hello my parent okay and then click Send and as you can see now we have that message on our parent so if I type for example how are you my child okay and then click Send here we have that message how are you my child so let's say good how are you my parent okay and then I click Send and we get that message back to our parent so that is how you implement the logic of delegate callback pattern so again steps are the following step one create a function on your parent form step two pass that function to your child form with the help of function pointers or delegates step 3 store that function pointer on your child form in a global variable in a global function pointer so that you can invoke it anywhere in that class and then step four invoke it when you need it and in our situation that was on our button click when the user types a message and clicks send one thing that I promise to explain at the end of the video is the following please navigate to parent.cpp and take a look at this part here where we are creating our child form we are invoking its Constructor so here we have passed null PTR for a parameter that is called the component owner so if you take a look in your child component you will see the name of that parameter its owner and what this owner represents is that it represents the object that has created our form and in this situation who created our child form well the parent form created our child form so through this object we are going to have direct access to our parent form now this is something that you get with C plus plus Builder and this is one way that you can establish the communication between child and parent but unfortunately when you're working on real projects in real life in most situations you will not have this luxury and you will not have access to this so you will have to build delegate callback pattern from scratch so that is the reason why I showed you how you can build it from scratch and if you want to play with this and if you want to make this work um one tip that I'm going to give you is go back to your parent.cpp and here where you passed no PTR as the owner of your child form instead of passing no PTR you are going to pass this pointer and this pointer means that you are passing this form which is parent form so that is an interesting fact that I promised to explain at the beginning of the video but again do not rely that much on this because in most situations you will have to absolutely understand delegate callback pattern in order to be able to read the code and you will also find yourself in many situations where you yourself have to write it from scratch so that will be all for this video if you have any questions you can leave those in the comment section if you need additional resources to learn programming I am going to put some links down below and then if you enjoyed this video if you found it helpful make sure to give it a big thumbs up for the YouTube algorithm so that it knows to recommend it to other people who want to learn programming as well so thank you very much for watching and I am going to see you in some other video bye

Need a transcript for another video?

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

Function pointers, delegates and callbacks | Beginner fri...