I'm an inexperienced coder, trying to see where I would use template functions or template classes. It would be easiest for me to understand their applications in my own program (which I outline below), however any real-life use-cases would be greatly appreciated.
My Application
I currently have a videogame level-builder application that I've created using SDL2 and C++. I've been mostly using polymorphic classes and adding components to them as needed via unique_ptrs or shared_ptrs to extend functionality. I check via a nullptr check to see if a component has been uploaded to the object, if so, I run the relevant function.
For example when it comes to Videogame World Objects (objects to be placed in the videogame world):
A "base object" would just have a location and a basic render component that draws a colored rectangle.
A "derived object" would contain sprite sheet information and a different render component which would draw the relevant frame at its location.
In the application, I currently have a vector of "base object" shared_ptrs where I can add either "base object" or "derived object". I can then loop and call (*it)->fixed_update() to update every loop or (*it)->render() to draw every loop. And since fixed_update() and render() are virtual functions, the correct function per object is called.
I've read that one of the greatest strengths of C++ is templates. And, the more advanced C++ code I have seen uses templates extensively. So, I've been trying to update my coding methodology to use templates where possible.
I know technically by using vectors I am using pre-built templates. But, I was trying to figure out a way of using/creating my own class template or function template.
Function Template
My understanding of function templates is that they allow you to use the same function across different objects without rewriting the calls. An example I've seen a lot is being able to see the max of two numbers, whether int/double/etc.. can be done once by using a template, because the operator '>' is valid for all such objects.
In my case since 'fixed_update()' and 'render()' is valid for all objects, I would be able to call them via a template function.
Essentially, I could use a function template where I would pass in any object and have it call "fixed_update()" or "render()" as appropriate. Unfortunately, I don't really see how I've saved any time or made anything more efficient by doing so. I suppose the "fixed_update()" and "render()" functions wouldn't need to be virtual, but they would still need to be written twice (once in each class) and then called in my game loop.
Class Template
My understanding of class templates is that they allow you to have a collection of same-type objects, and organize/manipulate them. I couldn't really see an application for this unless I wanted my own custom data structure. I can't really see where I would want one though.
Conclusion
So to sum up, I'm just struggling understanding where and when I should be writing function templates and/or class templates. If anyone has any pointers on what I should do with my code or other real-life use cases, it would be greatly appreciated.
Aucun commentaire:
Enregistrer un commentaire