From 6f346f3357c8759ebb5a4e980d056fda5e27abd8 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Sun, 9 Feb 2025 21:30:54 -0500 Subject: [PATCH] Two possible ways to fix drawable to avoid the const problem, but not quite right. --- meson.build | 6 +++++- sfml-const/{fixed.cpp => fix1.cpp} | 4 ++-- sfml-const/{fixed.hpp => fix1.hpp} | 2 +- sfml-const/fix2.cpp | 26 ++++++++++++++++++++++++++ sfml-const/fix2.hpp | 27 +++++++++++++++++++++++++++ sfml-const/original.cpp | 2 +- 6 files changed, 62 insertions(+), 5 deletions(-) rename sfml-const/{fixed.cpp => fix1.cpp} (68%) rename sfml-const/{fixed.hpp => fix1.hpp} (78%) create mode 100644 sfml-const/fix2.cpp create mode 100644 sfml-const/fix2.hpp diff --git a/meson.build b/meson.build index ae0dfb3..813c184 100644 --- a/meson.build +++ b/meson.build @@ -54,6 +54,10 @@ executable('sfml-original', 'sfml-const/original.cpp', override_options: exe_defaults, dependencies: dependencies) -executable('sfml-fixed1', 'sfml-const/fixed.cpp', +executable('sfml-fix1', 'sfml-const/fix1.cpp', + override_options: exe_defaults, + dependencies: dependencies) + +executable('sfml-fix2', 'sfml-const/fix2.cpp', override_options: exe_defaults, dependencies: dependencies) diff --git a/sfml-const/fixed.cpp b/sfml-const/fix1.cpp similarity index 68% rename from sfml-const/fixed.cpp rename to sfml-const/fix1.cpp index 338273a..6cfcf02 100644 --- a/sfml-const/fixed.cpp +++ b/sfml-const/fix1.cpp @@ -1,9 +1,9 @@ #include -#include "constness.hpp" +#include "fix1.hpp" using std::cout; -void Shape::draw(RenderTarget& target) const { +void Shape::draw(RenderTarget& target) { cout << "shape draw\n"; } diff --git a/sfml-const/fixed.hpp b/sfml-const/fix1.hpp similarity index 78% rename from sfml-const/fixed.hpp rename to sfml-const/fix1.hpp index 54e18c5..41ac504 100644 --- a/sfml-const/fixed.hpp +++ b/sfml-const/fix1.hpp @@ -12,5 +12,5 @@ class Drawable { class Shape : public Drawable { public: - void draw(RenderTarget& target) const override; + void draw(RenderTarget& target) override; }; diff --git a/sfml-const/fix2.cpp b/sfml-const/fix2.cpp new file mode 100644 index 0000000..3faf153 --- /dev/null +++ b/sfml-const/fix2.cpp @@ -0,0 +1,26 @@ +#include +#include "fix2.hpp" + +using std::cout; + +void RenderTarget::draw(const Drawable& drawable) { + drawable.draw(*this); +} + +void Shape::draw(RenderTarget& target) const { + cout << "shape draw\n"; +} + +void Shape::render_to(RenderTarget &target) { + cout << "shape render_to instead\n"; +} + +int main() { + RenderTarget target; + Shape shape; + + target.draw(shape); + + shape.render_to(target); + return 0; +} diff --git a/sfml-const/fix2.hpp b/sfml-const/fix2.hpp new file mode 100644 index 0000000..75581d1 --- /dev/null +++ b/sfml-const/fix2.hpp @@ -0,0 +1,27 @@ +#pragma once + +class Drawable; + +class RenderTarget { + public: + void draw(const Drawable& drawable); +}; + +class Drawable { + public: + /* + * NOTE: This requires const because RenderTarget + * pass itself in as a *this which is always const. + * If you need to draw without const then invert it + * then use shape.render_to(target) instead. + */ + virtual void draw(RenderTarget& target) const = 0; + + virtual void render_to(RenderTarget& target) = 0; +}; + +class Shape : public Drawable { + public: + void draw(RenderTarget& target) const override; + void render_to(RenderTarget &target) override; +}; diff --git a/sfml-const/original.cpp b/sfml-const/original.cpp index 275ea98..618d567 100644 --- a/sfml-const/original.cpp +++ b/sfml-const/original.cpp @@ -1,5 +1,5 @@ #include -#include "constness.hpp" +#include "original.hpp" using std::cout;