Added a small micro example of the problem with SFML's use of const on drawable virtual functions which shows the _real_ reason they did this is because of a poor design decision to make both Drawable and RenderTarget equally in charge of drawing the other. AKA the 'Deadly Embrace' design flaw.

master
Zed A. Shaw 10 months ago
parent b8395c6d9f
commit 5f25383891
  1. 4
      meson.build
  2. 24
      sfml-const/constness.cpp
  3. 18
      sfml-const/constness.hpp

@ -49,3 +49,7 @@ executable('rvo_test', [
], ],
override_options: exe_defaults, override_options: exe_defaults,
dependencies: dependencies) dependencies: dependencies)
executable('constness', 'sfml-const/constness.cpp',
override_options: exe_defaults,
dependencies: dependencies)

@ -0,0 +1,24 @@
#include <iostream>
#include "constness.hpp"
using std::cout;
void RenderTarget::draw(const Drawable& drawable) {
drawable.draw(*this);
}
void Shape::draw(const RenderTarget& target) const {
cout << "shape draw\n";
}
using std::cout;
int main() {
RenderTarget target;
Shape shape;
target.draw(shape);
return 0;
}

@ -0,0 +1,18 @@
#pragma once
class Drawable;
class RenderTarget {
public:
void draw(const Drawable& drawable);
};
class Drawable {
public:
virtual void draw(const RenderTarget& target) const = 0;
};
class Shape : public Drawable {
public:
void draw(const RenderTarget& target) const override;
};
Loading…
Cancel
Save