On Nov 16, 12:03 pm, red floyd<no.spam.h...@its.invalid> wrote:
You are somewhat correct here. Have the "write" function return
a string which the XML code can display. By divorcing the I/O from
the representation return, you're more general anyways.
Red,
Thank you for your reply. I believe you have attempted to solve my
problem by modifying the original shapes to return their "XML
components" as strings.
Unfortunately this is not what I am interested in because it does not
scale to what I really want to do. Instead of writing a long-winded
response that might not come across correctly, allow me to change my
original question to use drawing instead of string writing:
===
Is it possible in C++ to modify "Library B" below to eliminate the
dynamic_cast and switch statements, while at the same time not doing
any of the following:
- Do not use DeviceContext in "Library A"
- Do not put drawing code in "Library A"
- Do not put the concept of drawing into "Library A" including adding
a class Shape::GetPixelsToDraw
My goal is to
a) not put any reference to drawing into Library A
b) not use dynamic_cast or switch/if blocks in Library B
The answer might be something like "use factories" or "use template"
or "this is not possible in c++".
Thank you,
Chris
// Library A
struct Shape { virtual ~Shape() {} };
struct Circle : public Shape { float radius; };
struct Square : public Shape { float edge; };
// Library B
#include<cmath>
struct DeviceContext { void FillPixel(int PixelX, int PixelY) {}; };
class Drawer
{
static void write(Shape* shape, DeviceContext& Dc)
{
if (Circle* circle = dynamic_cast<Circle*>(shape))
{
for (float Angle = 0; Angle< 2*3.14156; Angle += 0.1)
{
Dc.FillPixel(cos(Angle) * circle->radius, sin(Angle) * circle-
radius);
}
}
else if (Square* square = dynamic_cast<Square*>(shape))
{
Dc.FillPixel(0, 0);
Dc.FillPixel(square->edge, 0);
Dc.FillPixel(square->edge, square->edge);
Dc.FillPixel(0, square->edge);
}
}
};
virtual void draw(DeviceContext& Dc) { ... }