One more error using function pointers - void value not ignored as it ought to be
-
I am still confused what he is trying to do. Why does he need the function pointer internal to the class he can call any function from the inherited interfaces directly. You usually only do member function calls from a block of code outside the class.
In vino veritas
-
I am still confused what he is trying to do. Why does he need the function pointer internal to the class he can call any function from the inherited interfaces directly. You usually only do member function calls from a block of code outside the class.
In vino veritas
-
Yes, I can't quite figure out what is (supposed to be) going on, and none of the code snippets make it any clearer.
Thanks, really helpful to make me understand how function pointer works. The whole exercise is to be able to call from one class to another in the hierarchy. What I am doing is recoding an app which was written without classes, I call it "flat" for lack of other words. It is basically relations between embedded processor hardware devices - USB , USB host, UART ( print) , LCD and video camera ( USB device). I am trying to use compound inheritance with top class managing the process of getting video data to the LCD. It is interrupt driven and I think that is where I went wrong - I need to rebuild the inheritance relations to have the interrupt driven class as "mother ship". I am pretty much done , the last piece is to actually process the video buffer. That is where the function pointers are used. Thanks again, you guys are very helpful and I appreciate that,
-
Thanks, really helpful to make me understand how function pointer works. The whole exercise is to be able to call from one class to another in the hierarchy. What I am doing is recoding an app which was written without classes, I call it "flat" for lack of other words. It is basically relations between embedded processor hardware devices - USB , USB host, UART ( print) , LCD and video camera ( USB device). I am trying to use compound inheritance with top class managing the process of getting video data to the LCD. It is interrupt driven and I think that is where I went wrong - I need to rebuild the inheritance relations to have the interrupt driven class as "mother ship". I am pretty much done , the last piece is to actually process the video buffer. That is where the function pointers are used. Thanks again, you guys are very helpful and I appreciate that,
So the function call is from outside the class in the interrupt? That will be fun trying to make sure the class in a stable running state not starting or closing, before you start passing interrupts into it. You will get a nice big crash if you pass an interrupt into the class and it isn't stable :-)
In vino veritas
-
So the function call is from outside the class in the interrupt? That will be fun trying to make sure the class in a stable running state not starting or closing, before you start passing interrupts into it. You will get a nice big crash if you pass an interrupt into the class and it isn't stable :-)
In vino veritas
The original code has ISR collecting the (video ) data and when it detects end of frame another function does the actual video output to the LCD. I put the ISR code in one class and the output to LCD in another class. Now both classes inherited from Print class so I can do code debugging etc. There are other hardware inheritances - USB , SPI but that is all working. That is how the compound inheritance came to be, and that is basically working. The issue is the link between the ISR and LCD class. I was hoping for another upper layer of class to be the controlling class, but that seems to be convoluted way to do it. I think I have a better grasp on using function pointers and will try to just call from one class to another using pointers. BTW the hardware interrupt is continuous and there are some checks in the actual interrupt handler and ISR functions making sure it is running and getting correct data / USB packages. Thanks to the group I think I got the basics, now it is just a matter of organizing the puzzle.
-
The original code has ISR collecting the (video ) data and when it detects end of frame another function does the actual video output to the LCD. I put the ISR code in one class and the output to LCD in another class. Now both classes inherited from Print class so I can do code debugging etc. There are other hardware inheritances - USB , SPI but that is all working. That is how the compound inheritance came to be, and that is basically working. The issue is the link between the ISR and LCD class. I was hoping for another upper layer of class to be the controlling class, but that seems to be convoluted way to do it. I think I have a better grasp on using function pointers and will try to just call from one class to another using pointers. BTW the hardware interrupt is continuous and there are some checks in the actual interrupt handler and ISR functions making sure it is running and getting correct data / USB packages. Thanks to the group I think I got the basics, now it is just a matter of organizing the puzzle.
What you have just said above is what makes what you are doing weird to us. There is no need to inherit a print class just to print, you just hold a print class instance in your class data and initialize it and you can print to it at any time you need. You don't inherit things just so you can use them. You only inherit things you need to change the behaviour. Your class interface is just bloating up and you gain exactly zero as you could already print what you need using the original class. So let me ask the specific question does your class change the print interface it inherits in any way? If you answer no then you don't need to inherit it just create an instance of the class in your class.
In vino veritas
-
What you have just said above is what makes what you are doing weird to us. There is no need to inherit a print class just to print, you just hold a print class instance in your class data and initialize it and you can print to it at any time you need. You don't inherit things just so you can use them. You only inherit things you need to change the behaviour. Your class interface is just bloating up and you gain exactly zero as you could already print what you need using the original class. So let me ask the specific question does your class change the print interface it inherits in any way? If you answer no then you don't need to inherit it just create an instance of the class in your class.
In vino veritas
The idea was - lazy man way . As written originally - the LCD class was derived / inherited from class Print. All usage of class Print was just calling the Print methods via inheritance, directly. To do what you suggest , which make perfect sense, I would have to change my other classes using Print methods to use the Print object. That is why I though multiple inheritance would work. It does , but things get tangled. And yes, I know how to do "find and replace ". I also have these "chain" dependencies and not sure how to tackle that using your method. I guess going from "flat" implantation to OOP is not that easy, but I hope I have Lerner few things in process. Would using "friend" class be of any advantage? Not that I need another headache. And no, my usage of inheritance does not change anything in class itself , they all basically just change / modify the data for their usage.
-
The idea was - lazy man way . As written originally - the LCD class was derived / inherited from class Print. All usage of class Print was just calling the Print methods via inheritance, directly. To do what you suggest , which make perfect sense, I would have to change my other classes using Print methods to use the Print object. That is why I though multiple inheritance would work. It does , but things get tangled. And yes, I know how to do "find and replace ". I also have these "chain" dependencies and not sure how to tackle that using your method. I guess going from "flat" implantation to OOP is not that easy, but I hope I have Lerner few things in process. Would using "friend" class be of any advantage? Not that I need another headache. And no, my usage of inheritance does not change anything in class itself , they all basically just change / modify the data for their usage.
You said ... "To do what you suggest , which make perfect sense, I would have to change my other classes using Print methods to use the Print object." No you don't all you need to do is create a printer class at initialization, lets say you have a private variable printer class called "ThePrinter". All it means is at the moment you are calling the inherited methods of the printer class directly so xyz_func(); so instead of that it would become ThePrinter.xyz_Func(); Really from a code point it's that simple a small change in the constructor and the name of the held printer infront of the printer class methods you are directly calling. Besides making your interface a hell of a lot smaller and removing bloating code and data in your class it also allows you to do things you can't do like change the printer device on the fly or pass the printer class to another object or a block of code. If you want to share the printer, so two different classes can write to the exact same printer (which may be likely in your case), you get one class to make the printer and then simply make a function on that class so the other classes can get a pointer to the printer. So the class that has the printer does this via a function GetMyPrinter
class IHavePrinter{
public:
PrinterClass* GetMyPrinter(void); // Return the created printer is a public function
private:
PrinterClass ThePrinter; // The actual created printer in this classes constructor
}IHavePrinter:: PrinterClass* GetMyPrinter(void){
return(&ThePrinter); // Return pointer to the created printer
}That is the point of keeping classes detached so you can do things you can't possibly do if you bind them into your object inheritance and you stop the vicious cross connection you are getting. I think most of your dependency issues is actually cause by your inheritance scheme and wouldn't exist if you did it properly. You could just have exposed the printer object as a public but the few lines to pass it on a function means that later on if you want to track who is using the printer or know if it's in use you can modify that function to track that etc. So a bit of forward planning built into the concept and trying to keep the classes as detached as possible not accessing each others fields.
In vino veritas
-
You said ... "To do what you suggest , which make perfect sense, I would have to change my other classes using Print methods to use the Print object." No you don't all you need to do is create a printer class at initialization, lets say you have a private variable printer class called "ThePrinter". All it means is at the moment you are calling the inherited methods of the printer class directly so xyz_func(); so instead of that it would become ThePrinter.xyz_Func(); Really from a code point it's that simple a small change in the constructor and the name of the held printer infront of the printer class methods you are directly calling. Besides making your interface a hell of a lot smaller and removing bloating code and data in your class it also allows you to do things you can't do like change the printer device on the fly or pass the printer class to another object or a block of code. If you want to share the printer, so two different classes can write to the exact same printer (which may be likely in your case), you get one class to make the printer and then simply make a function on that class so the other classes can get a pointer to the printer. So the class that has the printer does this via a function GetMyPrinter
class IHavePrinter{
public:
PrinterClass* GetMyPrinter(void); // Return the created printer is a public function
private:
PrinterClass ThePrinter; // The actual created printer in this classes constructor
}IHavePrinter:: PrinterClass* GetMyPrinter(void){
return(&ThePrinter); // Return pointer to the created printer
}That is the point of keeping classes detached so you can do things you can't possibly do if you bind them into your object inheritance and you stop the vicious cross connection you are getting. I think most of your dependency issues is actually cause by your inheritance scheme and wouldn't exist if you did it properly. You could just have exposed the printer object as a public but the few lines to pass it on a function means that later on if you want to track who is using the printer or know if it's in use you can modify that function to track that etc. So a bit of forward planning built into the concept and trying to keep the classes as detached as possible not accessing each others fields.
In vino veritas
-
Thanks, so in my simple words - instead of using multiple inheritance in downstream fashion doing it this way is sort of in reverse - the printer class goes first. Your are very helpful and much appreciated. Thanks. Cheers Vaclav
Yes we are putting composition over inheritance, so our classes are trying to achieve multiple and flexible behaviour (reusing as much code as possible) rather than inheritance from a base or parent class. I would also add there is one final thing you will be able to do if you go that path which you can't possibly do with your current scheme, which is to thread the classes or at least the one that had the old ISR code to help its response and speed.
In vino veritas