From 9aa137291be418ef91486275fac727a04660e439 Mon Sep 17 00:00:00 2001 From: Igor Bogoslavskyi Date: Sun, 1 Dec 2024 16:52:21 +0100 Subject: [PATCH] Add a link to godbolt --- lectures/polymorphism_vs_templates.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lectures/polymorphism_vs_templates.md b/lectures/polymorphism_vs_templates.md index c77fd28..2e6784b 100644 --- a/lectures/polymorphism_vs_templates.md +++ b/lectures/polymorphism_vs_templates.md @@ -151,7 +151,7 @@ struct ImageLoaderInterface: public NonCopyableNonMoveable { virtual ~ImageLoaderInterface() = default; }; -struct PngImageLoader : public ImageLoaderInterface { +struct PngImageLoader final: public ImageLoaderInterface { std::optional LoadImage(const std::filesystem::path& path) const override { std::cout << "Loading PNG image." << std::endl; // Irrelevant here logic for actually loading the image. @@ -159,7 +159,7 @@ struct PngImageLoader : public ImageLoaderInterface { } }; -struct JpegImageLoader : public ImageLoaderInterface { +struct JpegImageLoader final: public ImageLoaderInterface { std::optional LoadImage(const std::filesystem::path& path) const override { std::cout << "Loading JPEG image." << std::endl; // Irrelevant here logic for actually loading the image. @@ -287,6 +287,8 @@ int main() { } ``` + + ## We can use templates to achieve the same The method above uses dynamic polymorphism to achieve what it does. That means that the type conversion and selection of the run path is happening at runtime. One alternative way to address the same problem is to use static polymorphism instead by using templates.