parent
43e3fb8582
commit
6f346f3357
@ -1,9 +1,9 @@ |
|||||||
#include <iostream> |
#include <iostream> |
||||||
#include "constness.hpp" |
#include "fix1.hpp" |
||||||
|
|
||||||
using std::cout; |
using std::cout; |
||||||
|
|
||||||
void Shape::draw(RenderTarget& target) const { |
void Shape::draw(RenderTarget& target) { |
||||||
cout << "shape draw\n"; |
cout << "shape draw\n"; |
||||||
} |
} |
||||||
|
|
@ -0,0 +1,26 @@ |
|||||||
|
#include <iostream> |
||||||
|
#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; |
||||||
|
} |
@ -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; |
||||||
|
}; |
Loading…
Reference in new issue