Skip to content

Commit

Permalink
Add a link to godbolt
Browse files Browse the repository at this point in the history
  • Loading branch information
niosus committed Dec 1, 2024
1 parent 9e89b81 commit 9aa1372
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lectures/polymorphism_vs_templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,15 +151,15 @@ struct ImageLoaderInterface: public NonCopyableNonMoveable {
virtual ~ImageLoaderInterface() = default;
};
struct PngImageLoader : public ImageLoaderInterface {
struct PngImageLoader final: public ImageLoaderInterface {
std::optional<Image> LoadImage(const std::filesystem::path& path) const override {
std::cout << "Loading PNG image." << std::endl;
// Irrelevant here logic for actually loading the image.
return Image{};
}
};
struct JpegImageLoader : public ImageLoaderInterface {
struct JpegImageLoader final: public ImageLoaderInterface {
std::optional<Image> LoadImage(const std::filesystem::path& path) const override {
std::cout << "Loading JPEG image." << std::endl;
// Irrelevant here logic for actually loading the image.
Expand Down Expand Up @@ -287,6 +287,8 @@ int main() {
}
```
<!-- https://godbolt.org/z/M83dxK4aE -->
## 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.
Expand Down

0 comments on commit 9aa1372

Please sign in to comment.