Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How can I remove dynamic_cast and if statements from this code snippet?

76 views
Skip to first unread message

Chris Stankevitz

unread,
Nov 16, 2011, 1:37:06 PM11/16/11
to
Hello,

I would like to remove the "dynamic_cast" and "if" statements from the
code below. I believe some people would describe this as making the
code "polymorphic". Can you recommend a way to do this without
modifying the original classes in "Library A"?

My intention is to create an "XML Writer" class(es) for shapes without
putting "XML code" in the base classes. I plan to use a similar
pattern for drawing and other tasks my application has to perform on
Shape objects.

Thank you,

Chris

// Library A
struct Shape { virtual ~Shape() {} };
struct Circle : public Shape { float radius; };
struct Square : public Shape { float edge; };

// Library B
#include <iostream>

class XmlWriter
{
static void write(Shape* shape)
{
if (Circle* circle = dynamic_cast<Circle*>(shape))
{
std::cout << "<Circle Radius=" << circle->radius << "/>";
}
else if (Square* square = dynamic_cast<Square*>(shape))
{
std::cout << "<Square Edge=" << square->edge << "/>";
}
}
};

Ian Collins

unread,
Nov 16, 2011, 1:52:41 PM11/16/11
to
Something like:

struct Shape {
virtual void write() = 0;
};
struct Circle : public Shape {
float radius;
void write()
{
std::cout<< "<Circle Radius="<< circle->radius<< "/>";
}
};

struct Square : public Shape {
float edge;
void write()
{
std::cout<< "<Square Edge="<< square->edge<< "/>";
}
};

class XmlWriter
{
static void write(Shape* shape)
{
shape->write();
}
};

--
Ian Collins

Chris Stankevitz

unread,
Nov 16, 2011, 2:14:46 PM11/16/11
to
On Nov 16, 10:52 am, Ian Collins <ian-n...@hotmail.com> wrote:
> Something like:
>
> struct Shape {
>    virtual void write() = 0;};

Ian,

I prefer/require a solution in which the shapes themselves are not
writing out XML -- I want the XML related code restricted to the
"XmlWriter" class in "Library B". This appears to be an overly
draconian requirement in this simple example, but should make more
sense if you consider adding something like a "drawing" or "network"
class that would introduce dependencies on drawing or socket code.

I want drawing/xml/socket code relegated to "Library B" without
compile or link dependencies in "Library A".

Thank you,

Chris

red floyd

unread,
Nov 16, 2011, 3:03:39 PM11/16/11
to
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.

BTW, what book are you reading? This fragment/problem is actually
one of the canonical OOP examples.

class Shape {
public:
//...
virtual std::string as_string() = 0;
}

class Square {
//...
std::string as_string() { return "Edge..." };
}

class

Leigh Johnston

unread,
Nov 16, 2011, 3:35:16 PM11/16/11
to
Something like the following perhaps?

#include <iostream>
#include <string>
#include <vector>
#include <memory>

struct serializer
{
virtual void write(const std::string& shapeName, const std::string&
dimensionName, float dimensionValue) const = 0;
};

struct shape
{
virtual void write(const serializer& writer) const = 0;
};

struct circle : shape
{
circle(float radius) : radius(radius) {}
virtual void write(const serializer& writer) const
{
writer.write("Circle", "Radius", radius);
}
float radius;
};

struct square : shape
{
square(float edge) : edge(edge) {}
virtual void write(const serializer& writer) const
{
writer.write("Square", "Edge", edge);
}
float edge;
};

struct xml_writer : serializer
{
virtual void write(const std::string& shapeName, const std::string&
dimensionName, float dimensionValue) const
{
std::cout << "<" << shapeName << " " << dimensionName << "=\"" <<
dimensionValue << "\" />\n";
}
};

int main()
{
typedef std::tr1::shared_ptr<shape> shape_ptr;
typedef std::vector<shape_ptr> shapes_t;
shapes_t shapes;
shapes.push_back(shape_ptr(new circle(7.0f)));
shapes.push_back(shape_ptr(new square(42.0f)));
xml_writer w;
for (shapes_t::const_iterator i = shapes.begin(); i != shapes.end(); ++i)
(**i).write(w);
}

Chris Stankevitz

unread,
Nov 16, 2011, 4:28:36 PM11/16/11
to
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);
}
}
};

Ian Collins

unread,
Nov 16, 2011, 4:37:11 PM11/16/11
to
Well you have to look seriously at whether a shape knows how to write
its self, or an XmlWriter knows how to write a shape. If it's the
latter, you gave to provide a means for an XmlWriter to access the
required information. This can lead to significant complexity and
coupling in your design.

It gets more complex still if you consider drawing a shape.

--
Ian Collins

Leigh Johnston

unread,
Nov 16, 2011, 4:39:38 PM11/16/11
to
// Library B

struct Drawable
{
virtual void draw(DeviceContext& Dc) const = 0
};

struct DrawableCircle : Circle, Drawable
{
virtual void draw(DeviceContext& Dc) { ... }
};

void Drawer::write(Drawable& drawable, DeviceContext& Dc)
{
drawable.draw(Dc);
}

Perhaps?

/Leigh

Leigh Johnston

unread,
Nov 16, 2011, 4:40:28 PM11/16/11
to
Oops forgot a "const" there; spot it?

/Leigh

Leigh Johnston

unread,
Nov 16, 2011, 4:43:47 PM11/16/11
to
Or if you don't want to derive from library A classes:

struct Drawable
{
virtual void draw(DeviceContext& Dc) const = 0
};

template <typename Shape>
struct DrawableShape : Drawable
{
Shape& shape;
DrawableShape(Shape& shape) : shape(shape) {}
virtual void draw(DeviceContext& Dc) const { /* use shape and Dc here
*/ }
};

void Drawer::write(Drawable& drawable, DeviceContext& Dc)
{
drawable.draw(Dc);
}

HTH

/Leigh

Leigh Johnston

unread,
Nov 16, 2011, 4:45:08 PM11/16/11
to
Obviously you would have to specialize draw for different shape types.

HTH.

/Leigh

Chris Stankevitz

unread,
Nov 16, 2011, 4:46:19 PM11/16/11
to
On Nov 16, 1:39 pm, Leigh Johnston <le...@i42.co.uk> wrote:
> struct DrawableCircle : Circle, Drawable

Unfortunately the object that creates my shapes cannot create
DrawableShapes as that library has no dependency on or knowledge of
the concept of drawing. But thank you for continuing to attempt to
help me.

Chris

Leigh Johnston

unread,
Nov 16, 2011, 4:49:11 PM11/16/11
to
Yeah I wasn't really thinking/trying hard; mostly noise sorry.

/Leigh

Chris Stankevitz

unread,
Nov 16, 2011, 4:41:47 PM11/16/11
to
On Nov 16, 1:37 pm, Ian Collins <ian-n...@hotmail.com> wrote:
> Well you have to look seriously at whether a shape knows how to write
> its self, or an XmlWriter knows how to write a shape.  If it's the
> latter, you have to provide a means for an XmlWriter to access the
> required information.

Yes, it is the latter that I would like to do.

> This can lead to significant complexity

Yes, I am looking for complexity and am curious if it is possible to
do this with c++.

> It gets more complex still if you consider drawing a shape.

Yes, I am trying to draw Shapes without Shapes knowing how to draw
themselves. Is it possible to do this in c++ without using
dynamic_cast or lots of switch/if statements?

Thank you,

Chris

Ian Collins

unread,
Nov 16, 2011, 4:56:36 PM11/16/11
to
The problem has little to do with C++. The issue of who knows how to do
what is common to any language.

The literal answer to your question is yes. You can always replace
conditionals with virtual member functions.

--
Ian Collins

Chris Stankevitz

unread,
Nov 16, 2011, 5:03:23 PM11/16/11
to
On Nov 16, 1:56 pm, Ian Collins <ian-n...@hotmail.com> wrote:
> You can always replace conditionals with virtual member functions.

I assume in the drawing case you are suggesting I add a virtual
function "GetVerticies" or a virtual function "Draw". This is now
what I want for these reasons:

1. I have to modify the Shape class to be aware that it is being drawn
2. Some shapes cannot be drawn with "verticies" and need to be drawn
carefully and uniquely -- for example a doughnut that has a hole in
the middle.

Thank you,

Chris

Ian Collins

unread,
Nov 16, 2011, 5:12:16 PM11/16/11
to
So how would a generic drawing class be expected to know this? It would
have to changed for each new shape.

Does your window manager know how to render every type of window on your
system?

--
Ian Collins

Chris Stankevitz

unread,
Nov 16, 2011, 4:38:52 PM11/16/11
to
On Nov 16, 12:35 pm, Leigh Johnston <le...@i42.co.uk> wrote:
>         virtual void write(const serializer& writer) const


Leigh,

Thank you for your reply. I believe you have effectively modified the
base class to return the XML components as strings. This is not what
I am attempting to do as it requires me to modify the base class to
assume it will be serialized. I'm looking for an approach in which the
base class is not modified or at least is modified only in a way that
allows a Factory or Template or some other technique to give shape
details to another library. I do not want to modify the base class to
share "serializing" details. I try to explain this a little better in
my reply to Red.

Thank you,

Chris

Chris Stankevitz

unread,
Nov 16, 2011, 6:20:53 PM11/16/11
to
On Nov 16, 2:12 pm, Ian Collins <ian-n...@hotmail.com> wrote:
> So how would a generic drawing class be expected to know this?

This is basically the question I am asking this group.

Below is a pseudocode answer to the question that
- leaves Shape, Circle, and Square untouched (good)
- places no drawing dependency on the shapes (good)
- leaves drawing code entirely in the Drawer classes (good)
- does not have switch or if statements (good)
* does use a dynamic cast (BAD)

Can you (or anyone else) come up with some c++ pseudocode that
- leaves Shape, Circle, and Square untouched (good)
- places no drawing dependency on the shapes (good)
- leaves drawing code entirely in the Drawer classes (good)
- does not have switch or if statements (good)
- does not use a dynamic cast (good)

Chris

=====

//----------
struct Drawer
{
virtual void Draw(Shape* shape) = 0;
};

//----------
struct CircleDrawer : public Drawer
{
void Draw(Shape* shape)
{
// dynamic_cast<Circle*>(shape)
// draw the circle
}
};

//----------
struct SquareDrawer : public Drawer
{
void Draw(Shape* shape)
{
// dynamic_cast<Square*>(shape)
// draw the square
}
};

//----------
struct DrawerCreator
{
virtual Drawer* GetNewDrawer() = 0;
virtual bool CanDraw(Shape* shape) = 0;
};

//----------
struct CircleDrawerCreator : public DrawerCreator
{
Drawer* GetNewDrawer() { return new CircleDrawer; }
bool CanDraw(Shape* shape) { return dynamic_cast<Circle*>(shape); }
};

//----------
struct SqareDrawerCreator : public DrawerCreator
{
Drawer* GetNewDrawer() { return new SquareDrawer; }
bool CanDraw(Shape* shape) { return dynamic_cast<Square*>(shape); }
};

//----------
struct DrawerManager
{
void RegisterDrawerCreator(DrawerCreator* dc)
{
drawers.push_back(dc);
}

void Draw(Shape* shape)
{
if (Drawer* drawer = GetDrawer(shape))
{
drawer->Draw(shape);
}
}

Drawer* GetDrawer(Shape* shape)
{
for (int i = 0; i < drawers.size(); ++i)
{
if (drawers[i]->CanDraw(shape))
{
return drawers[i]->GetNewDrawer();
}
}

return 0;
}

vector<Drawer*> drawers;
};

//----------
int main()
{
DrawerManager drawer_manager;

drawer_manager.RegisterDrawerCreator(new CircleDrawerCreator);
drawer_manager.RegisterDrawerCreator(new SquareDrawerCreator);

Shape* shape1 = new Circle;
Shape* shape2 = new Square;

drawer_manager.Draw(shape1);
drawer_manager.Draw(shape2);
}

Werner

unread,
Nov 17, 2011, 2:39:42 AM11/17/11
to
On Nov 17, 1:20 am, Chris Stankevitz <chrisstankev...@gmail.com>
wrote:

> Below is a pseudocode answer to the question that
>  - leaves Shape, Circle, and Square untouched (good)
>  - places no drawing dependency on the shapes (good)
>  - leaves drawing code entirely in the Drawer classes (good)
>  - does not have switch or if statements (good)
>  * does use a dynamic cast (BAD)
>
> Can you (or anyone else) come up with some c++ pseudocode that
>  - does not use a dynamic cast (good)

I can't see why using dynamic cast is so bad. It is not as
if you have an if/else style of programming that requires
constant modification. dynamic_cast under your control that
requires no modification is good (except for being a tid
slower, perhaps).

You might want to remove the burden from your client though (see
below):

#include <vector>

struct Shape{};
struct Circle : Shape{};
struct Square : Shape{};

struct AbstractDrawer
{
virtual void Draw(Shape* shape) = 0;

};

template <class ShapeT>
struct TypedDrawer : AbstractDrawer
{
typedef ShapeT shape_type;

protected:
virtual void DoDraw( shape_type& shape ) = 0;
//{ Default implementation ??? }

private:
virtual void Draw( Shape* shape )
{
DoDraw( dynamic_cast<shape_type&>(*shape) );
}
};

struct CircleDrawer : TypedDrawer<Circle>
{
protected:
virtual void DoDraw( Circle& shape )
{
//Implementation
}
};

struct SquareDrawer : TypedDrawer<Square>
{
protected:
virtual void DoDraw( Square& shape )
{
//Implementation
}
};

//----------
struct AbstractDrawerCreator
{
virtual AbstractDrawer* GetNewDrawer() = 0;
virtual bool CanDraw(Shape* shape) = 0;
};

template <class DrawerT>
struct TypedDrawerCreator : AbstractDrawerCreator
{
private:
typedef DrawerT drawer_type;
typedef typename DrawerT::shape_type shape_type;

virtual drawer_type* GetNewDrawer() const
{
return new drawer_type;
}
virtual bool CanDraw( Shape* shape ) const
{
return dynamic_cast<shape_type*>(shape);
}
};

typedef TypedDrawerCreator<CircleDrawer> CircleDrawerCreator;
typedef TypedDrawerCreator<SquareDrawer> SquareDrawerCreator;
//etc...


//[Werner Erasmus: Left this in as I've removed compiler
// errors]
struct DrawerManager
{
void RegisterDrawerCreator(AbstractDrawerCreator* dc)
{
drawers.push_back(dc);
}

void Draw(Shape* shape)
{
if (AbstractDrawer* drawer = GetDrawer(shape))
{
drawer->Draw(shape);
}
}

AbstractDrawer* GetDrawer(Shape* shape)
{
for (int i = 0; i < drawers.size(); ++i)
{
if (drawers[i]->CanDraw(shape))
{
return drawers[i]->GetNewDrawer();
}
}

return 0;
}

std::vector<AbstractDrawerCreator*> drawers;
};



Francesco

unread,
Nov 17, 2011, 4:56:02 AM11/17/11
to
Hi,

IMHO you can try one of the following approaches:
- map the type_info of the classes to some function that does the
actual work, any map will give you on the average better than linear
behavior (cascade of ifs). This applies only if you have lots of
classes, if you have only a handful it will be worse (probably).
- create a mirror hierarchy of classes (deriving or composing). Add
the virtual write method on those classes and just use those instead
of the original ones...

Obviously if you could modify the original classes, that would be the
way to go...
Bye,
Francesco

// CODE


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Library A
struct Shape { virtual ~Shape() {} };
struct Circle : public Shape { float radius; };
struct Square : public Shape { float edge; };

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Library B
#include <iostream>
#include <cassert>

#define USE_UNORDERED_MAP

#ifdef USE_UNORDERED_MAP
#include <tr1/unordered_map>
#else
#include <map>
#endif

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Type unsafe functions!!

void WriteCircle( Shape * shape )
{
Circle * circle = static_cast< Circle * >( shape );
std::cout << "<Circle Radius=" << circle->radius << "/>";
}

void WriteSquare( Shape * shape )
{
Square * square = static_cast< Square * >( shape );
std::cout << "<Square Edge=" << square->edge << "/>";
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// TypeInfo class for map key

class CTypeInfo
{
public:
CTypeInfo( std::type_info const & inTypeInfo )
: mTypeInfoPtr( &inTypeInfo )
#ifdef USE_UNORDERED_MAP
, mHash( std::tr1::hash< std::string >()
( inTypeInfo.name() ) )
#endif
{ }

bool operator<( CTypeInfo const & inTypeInfo ) const
{ return mTypeInfoPtr->before( *inTypeInfo.mTypeInfoPtr ); }

bool operator==( CTypeInfo const & inTypeInfo ) const
{ return *mTypeInfoPtr == *inTypeInfo.mTypeInfoPtr; }

#ifdef USE_UNORDERED_MAP
size_t GetHash( void ) const
{ return mHash; }
#endif

private:
std::type_info const * mTypeInfoPtr;
#ifdef USE_UNORDERED_MAP
size_t mHash;
#endif

};

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Map stuff

#ifdef USE_UNORDERED_MAP
struct CTypeInfoHash
{
size_t operator()( CTypeInfo const & inTypeInfo ) const
{ return inTypeInfo.GetHash(); }
};
#endif

typedef void (*tFuncPtr)( Shape * );
#ifdef USE_UNORDERED_MAP
typedef std::tr1::unordered_map< CTypeInfo, tFuncPtr, CTypeInfoHash >
CMap;
#else
typedef std::map< CTypeInfo, tFuncPtr > CMap;
#endif


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// XmlWriter

class XmlWriter
{
private:
// this is all thread unsafe!!!
static CMap & GetMap()
{
static CMap sMap;
return sMap;
}

static void Init()
{
static bool sInit = false;
if( !sInit )
{
CMap & map = GetMap();
map.insert( CMap::value_type( typeid( Circle ),
&WriteCircle ) );
map.insert( CMap::value_type( typeid( Square ),
&WriteSquare ) );
sInit = true;
}
}
public:
void write(Shape* shape)
{
Init();
CMap & map = GetMap();
CMap::const_iterator iter = map.find( typeid( *shape ) );
assert( iter != map.end() );
iter->second( shape );
}
};

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// augmenting classes
// WARNING: quick example, many things to look after...

struct CShapePlus
{
virtual ~CShapePlus() {}
virtual void Write() const = 0;
};

// composition

struct CCirclePlus : public CShapePlus
{
Circle mCircle;
virtual void Write() const
{ std::cout << "<Circle Radius=" << mCircle.radius << "/>"; }

};

struct CSquarePlus : public CShapePlus
{
Square mSquare;
virtual void Write() const
{ std::cout << "<Square Edge=" << mSquare.edge << "/>"; }

};

// inheritance

struct CCirclePlus2 : public Circle, public CShapePlus
{
virtual void Write() const
{ std::cout << "<Circle Radius=" << this->radius << "/>"; }
};

struct CSquarePlus2 : public Square, public CShapePlus
{
virtual void Write() const
{ std::cout << "<Square Edge=" << this->edge << "/>"; }
};



//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// main

int main()
{
Shape * shape1 = new Circle;
Shape * shape2 = new Square;

XmlWriter writer;

writer.write( shape1 ); std::cout << '\n';
writer.write( shape2 ); std::cout << '\n';


CShapePlus * shape3 = new CCirclePlus;
CShapePlus * shape4 = new CSquarePlus;
CShapePlus * shape5 = new CCirclePlus2;
CShapePlus * shape6 = new CSquarePlus2;

shape3->Write(); std::cout << '\n';
shape4->Write(); std::cout << '\n';
shape5->Write(); std::cout << '\n';
shape6->Write(); std::cout << '\n';
}

// END CODE


Goran

unread,
Nov 17, 2011, 8:17:44 AM11/17/11
to
On Nov 17, 12:20 am, Chris Stankevitz <chrisstankev...@gmail.com>
wrote:
What you seem to be showing here is the adapter pattern. The way you
went about it seems backwards, though. When you create your adapter
(Drawer), you should strive to do it using the correct adaptee type.
But what you did is to throw said type back to base (by passing Shape*
to DrawerManager) and reached for dynamic_cast.

So how about this:

int main()
{
Canvas c;

Circle shape1;
Square shape2;

Draw(c, CircleDrawer(shape1));
Draw(c, SquareDrawer(shape2));
}

class Canvas { /*Driving primitives here*/ };

class Drawer
{
public: virtual void Draw(Canvas& c) const = 0;
};

void Draw(Canvas& c, Drawer& d)
{
d.Draw(c);
}

// Trivial intermediary: drawer for Shapes.
class ShapeDrawer
{
const Shape& s_;
public:
ShapeDrawer(Shape& s) : s_(s) {}
};

// Implementation starts here

class CircleDrawer : public ShapeDrawer
{
public:
CircleDrawer(const Circle& s) : ShapeDrawer(c) {}
virtual void Draw(Canvas& c) const {knock yourself out}
};

class SquareDrawer {you get the picture};

IOW... __Don't__ use generic Shapes in lib B logic. __Adapt__ them as
soon as you get them (because then you should know their types, and
that's the only way to avoid casting) and use adapters. Added bonus:
adapters will let you mix your own stuff with lib A stuff.

Goran.

P.S. You didn't want dynamic_cast, but rather static_cast, there. ;-)

Goran

unread,
Nov 17, 2011, 8:42:01 AM11/17/11
to
On Nov 17, 8:39 am, Werner <wer...@gmail.com> wrote:
> I can't see why using dynamic cast is so bad. It is not as
> if you have an if/else style of programming that requires
> constant modification. dynamic_cast under your control that
> requires no modification is good (except for being a tid
> slower, perhaps).

Well, conceptually, there's no difference between if/else and
dynamic_cast (and indeed OP's first post is dynamic_cast with if/
else). dynamic_cast merely moves decision to the type itself (whereas
if/else decides on the data). IOW, with dynamic_cast, type is used as
data. And indeed, if new type introduced, conditional logic must be
changed with itand therefore open/closed principle is compromised.

Goran.

Chris Stankevitz

unread,
Nov 17, 2011, 1:27:09 PM11/17/11
to
Werner,

Thank you for your reply.

> I can't see why using dynamic cast is so bad.

Perhaps it is not bad. One way or another I am going to complete this
task and I know I can complete it using dynamic_casts so I might end
up using them. I suppose my question to the group is "is it possible
to do something like this without using a dynamic_cast." The answer
to that question might be "no" -- which I'm okay with.

> You might want to remove the burden from your client though

Thank you your method is certainly cleaner than mine. I need to look
at it a bit more closely but I believe we are both doing the same
thing.

Chris

Chris Stankevitz

unread,
Nov 17, 2011, 1:31:26 PM11/17/11
to

Francesco,

Thank you for your reply.

> - map the type_info of the classes to some function

This is the sort of thing I came to the group to hear. I've never
heard of type_info before, so I am going to go learn about it. Just
from the name my suspicion is that type_info is doing the same thing
as dynamic_cast under the covers, but I'm going to find out.

Thank you for providing a novel solution to my problem that satisfies
my conditions!

Chris

Chris Stankevitz

unread,
Nov 17, 2011, 1:35:02 PM11/17/11
to

Goran,

On Nov 17, 5:17 am, Goran <goran.pu...@gmail.com> wrote:
> What you seem to be showing here is the adapter pattern. The way you
> went about it seems backwards, though.

Thank you this is exactly the kind of information I was looking for.
I'm going to go read up on Adapters. I've never heard of 'adapter
pattern'.

> But what you did is to throw said type back to base (by passing Shape*
> to DrawerManager) and reached for dynamic_cast.

I noticed this after I posted and some others pointed that out here on
the list. I understand the concept and the idea behind sending the
correct type to the drawer classes.

Thank you,

Chris

Chris Stankevitz

unread,
Nov 17, 2011, 1:37:50 PM11/17/11
to
On Nov 17, 5:42 am, Goran <goran.pu...@gmail.com> wrote:
> Well, conceptually, there's no difference between if/else and
> dynamic_cast (and indeed OP's first post is dynamic_cast with if/
> else). dynamic_cast merely moves decision to the type itself (whereas
> if/else decides on the data). IOW, with dynamic_cast, type is used as
> data.

Goran,

In the quote above are you trying to say this:

1. dynamic_cast and if/else are basically the same (for the reasons
you stated)
2. If someone is opposed to solving a problem with a bunch of if/else
statements based on data, then
3. that person should also be opposed to solving the problem using
dynamic_cast

Thank you,

Chris

addre...@invalid.invalid

unread,
Nov 17, 2011, 2:21:17 PM11/17/11
to
Chris Stankevitz <chrisst...@gmail.com> wrote:
> Hello,
>
> I would like to remove the "dynamic_cast" and "if" statements from the
> code below. I believe some people would describe this as making the
> code "polymorphic". Can you recommend a way to do this without
> modifying the original classes in "Library A"?
>
> My intention is to create an "XML Writer" class(es) for shapes without
> putting "XML code" in the base classes. I plan to use a similar
> pattern for drawing and other tasks my application has to perform on
> Shape objects.
>
> Thank you,
>
> Chris
>
> // Library A
> struct Shape { virtual ~Shape() {} };
> struct Circle : public Shape { float radius; };
> struct Square : public Shape { float edge; };
>
> // Library B
> #include <iostream>
>
> class XmlWriter
> {
> static void write(Shape* shape)
> {
> if (Circle* circle = dynamic_cast<Circle*>(shape))
> {
> std::cout << "<Circle Radius=" << circle->radius << "/>";
> }
> else if (Square* square = dynamic_cast<Square*>(shape))
> {
> std::cout << "<Square Edge=" << square->edge << "/>";
> }
> }
> };

I think this is a typical usecase for the "visitor" pattern:

// Library A

class ShapeVisitor;
class Shape { public: virtual void accept(ShapeVisitor& visitor) = 0; };
class Circle : public Shape {
public:
virtual void accept(ShapeVisitor& visitor) { visitor.visit(*this); }
};
class Square : public Shape {
public:
virtual void accept(ShapeVisitor& visitor) { visitor.visit(*this); }
};
class ShapeVisitor {
virtual void visit(Square& square) = 0;
virtual void visit(Circle& circle) = 0;
};

// Library B
class XmlWriter : public ShapeVisitor {
virtual void visit(Square& square) {
// do something
}
virtual void visit(Circle& circle) {
// do something
}
void write(Shape& shape) {
shape.accept(*this);
}
};

Tobi

Leigh Johnston

unread,
Nov 17, 2011, 2:33:30 PM11/17/11
to
Best answer yet.

/Leigh

Chris Stankevitz

unread,
Nov 17, 2011, 5:09:53 PM11/17/11
to
On Nov 17, 11:21 am, <address...@invalid.invalid> wrote:
> I think this is a typical usecase for the "visitor" pattern:

Tobi, this is exactly what I was looking for. Case closed.

Thank you,

Chris

Werner

unread,
Nov 18, 2011, 2:23:12 AM11/18/11
to
On Nov 17, 8:37 pm, Chris Stankevitz <chrisstankev...@gmail.com>
wrote:
> On Nov 17, 5:42 am, Goran <goran.pu...@gmail.com> wrote:
>
> > Well, conceptually, there's no difference between if/else and
> > dynamic_cast (and indeed OP's first post is dynamic_cast with if/
> > else). dynamic_cast merely moves decision to the type itself (whereas
> > if/else decides on the data). IOW, with dynamic_cast, type is used as
> > data.
>
> Goran,
>
> In the quote above are you trying to say this:
>
> 1. dynamic_cast and if/else are basically the same (for the reasons
> you stated)

Wrong.


> 2. If someone is opposed to solving a problem with a bunch of if/else
> statements based on data, then
> 3. that person should also be opposed to solving the problem using
> dynamic_cast

Also wrong.

Firstly, as mentioned previously, one only needs dynamic_cast
if you have virtual inheritance. Static_cast will suffice most
of the time.

Secondly, there are many ways to kill a cat. Whether you use
names or casting, you're still comparing...

Thirdly, in your original example using dynamic_cast did
not violate OCP (and in my example even less).

Definition of OCP:

Open to extension, closed to modification:

Bad use of dynamic cast (open to modification):

if( dynamic_cast<Square*>(shape) )
{
}
else if( dynamic_cast<Circle*>(shape ) )
else if()
{
//etc...
}

Consider the acyclic visitor as example as described in
Agile Software Development (Robert C. Martin), where
dynamic_cast is put to good use.

Now, furthermore (Goran):

Show me how the use of dynamic cast in my example (or the
original example, for that matter), violates OCP. None
of the code using dynamic_cast (or static_cast as better
alternative) needs modification. OCP is not violated...

Do I need to modify existing classes to change logic?
No. Dynamic_cast usage does not imply OCP is violated,
only a certain style of dynamic_cast usage.

Kind regards,

Werner

Werner

unread,
Nov 18, 2011, 2:28:06 AM11/18/11
to
On Nov 17, 3:42 pm, Goran <goran.pu...@gmail.com> wrote:


> Well, conceptually, there's no difference between if/else and
> dynamic_cast (and indeed OP's first post is dynamic_cast with if/
> else). dynamic_cast merely moves decision to the type itself (whereas
> if/else decides on the data). IOW, with dynamic_cast, type is used as
> data. And indeed, if new type introduced, conditional logic must be
> changed with itand therefore open/closed principle is compromised.

No, conditional logic was not changed. The for loop ensured that...
But point to me how conditional logic changed by adding a new
class. Also (by referring to my example), show me how OCP is
violated by adding new drawers or shapes... For OCP to be
violated, code containing logic needs to change. The for
loop (whilst crude) suffices... When using a map, if/else
also exist (N times). You just don't need to modify it. The
same applies here. Therefore - nonsense.

Kind regards,

Werner

Werner

unread,
Nov 18, 2011, 2:30:55 AM11/18/11
to
On Nov 18, 12:09 am, Chris Stankevitz <chrisstankev...@gmail.com>
wrote:
> On Nov 17, 11:21 am, <address...@invalid.invalid> wrote:
>
> > I think this is a typical usecase for the "visitor" pattern:
>
> Tobi, this is exactly what I was looking for.  Case closed.

What happens when new shapes are added to the hierarchy?

Kind regards,

Werner

Goran

unread,
Nov 18, 2011, 4:25:45 AM11/18/11
to
On Nov 17, 7:37 pm, Chris Stankevitz <chrisstankev...@gmail.com>
wrote:
I guess you can put it that way.

Still, striving for polymorphism will (IMHO) lower the number "data-
based" if/else (or switch) decisions, even if some dynamic_cast stay.
Use of if/else where polymorphism would help is bad, dynamic_cast is
slightly better, appropriate abstraction (possibly well-known pattern,
if I can recognize it) is best ;-).

Goran.

Goran

unread,
Nov 18, 2011, 4:46:59 AM11/18/11
to
On Nov 18, 8:23 am, Werner <wer...@gmail.com> wrote:
> On Nov 17, 8:37 pm, Chris Stankevitz <chrisstankev...@gmail.com>
> wrote:
>
> > On Nov 17, 5:42 am, Goran <goran.pu...@gmail.com> wrote:
>
> > > Well, conceptually, there's no difference between if/else and
> > > dynamic_cast (and indeed OP's first post is dynamic_cast with if/
> > > else). dynamic_cast merely moves decision to the type itself (whereas
> > > if/else decides on the data). IOW, with dynamic_cast, type is used as
> > > data.
>
> > Goran,
>
> > In the quote above are you trying to say this:
>
> > 1. dynamic_cast and if/else are basically the same (for the reasons
> > you stated)
>
> Wrong.
>
> > 2. If someone is opposed to solving a problem with a bunch of if/else
> > statements based on data, then
> > 3. that person should also be opposed to solving the problem using
> > dynamic_cast
>
> Also wrong.
>
> Firstly, as mentioned previously, one only needs dynamic_cast
> if you have virtual inheritance. Static_cast will suffice most
> of the time.

Not really. Indeed, OP needs dynamic_cast and has no virtual
inheritance.

>
> Secondly, there are many ways to kill a cat. Whether you use
> names or casting, you're still comparing...
>
> Thirdly, in your original example using dynamic_cast did
> not violate OCP (and in my example even less).
>
> Definition of OCP:
>
> Open to extension, closed to modification:
>
> Bad use of dynamic cast (open to modification):
>
> if( dynamic_cast<Square*>(shape) )
> {}
>
> else if( dynamic_cast<Circle*>(shape ) )
> else if()
> {
>   //etc...
>
> }
>
> Consider the acyclic visitor as example as described in
> Agile Software Development (Robert C. Martin), where
> dynamic_cast is put to good use.
>
> Now, furthermore (Goran):
>
> Show me how the use of dynamic cast in my example (or the
> original example, for that matter), violates OCP. None
> of the code using dynamic_cast (or static_cast as better
> alternative) needs modification. OCP is not violated...
>
> Do I need to modify existing classes to change logic?
> No. Dynamic_cast usage does not imply OCP is violated,
> only a certain style of dynamic_cast usage.

I agree with you. TBH, I didn't look at your example much, nor was I
complaining about --your-- use of the dynamic_cast. I was complaining
about OP's use of it.

However, I think your use of dynamic_cast can be avoided, using the
same logic I applied in my first (I think) post. If your typed drawer
is --created-- with a reference to it's shape, then your DoDraw can be
using the correct type --without-- a cast at all (and Draw wouldn't
have a "shape" parameter at all). In my mind, you still made the same
mistake as the OP: you lost the original type of the Shape, and that
caused more code to be added.

BTW, your code breaks in unexpected ways if you derive from Square or
Circle: you might get SquareDrawer to draw, I dunno, DerivedSquare.

Goran.

Goran

unread,
Nov 18, 2011, 5:58:27 AM11/18/11
to
On Nov 17, 8:21 pm, <address...@invalid.invalid> wrote:
Visitor requires changing Shape and the rest of Lib A, which OP said
didn't want elsethread.

Goran.

Goran

unread,
Nov 18, 2011, 4:29:41 AM11/18/11
to
On Nov 17, 8:21 pm, <address...@invalid.invalid> wrote:
Hmmm... I was thinking of "visitor", but found it inappropriate
because OP said he doesn't want to change library A, and visitor
requires changing the base class.

Goran.

Bart v Ingen Schenau

unread,
Nov 18, 2011, 8:45:50 AM11/18/11
to
Then the ShapeVisitor and derived visitors must be extended to support the
new shape.
This is no different from the original code with an if-else ladder based on
the result from dynamic_cast, except that the compiler is better able to
help warn you if you forgot to update a visitor.

>
> Kind regards,
>
> Werner

Bart v Ingen Schenau

Tobias Müller

unread,
Nov 18, 2011, 11:56:47 AM11/18/11
to
Goran <goran...@gmail.com> wrote:
> Visitor requires changing Shape and the rest of Lib A, which OP said
> didn't want elsethread.
>
> Goran.

Fair point. However, when reading the rest of the thread I had the
impression that what the OP really wanted is not to leave Lib A unchanged,
but rather not to include any XML specific features.
And a vistior interface is quite abstract and useful for a library like A.

And apparently the OP is happy with that solution.

Tobi.

Noah Roberts

unread,
Nov 18, 2011, 12:23:17 PM11/18/11
to
On Nov 16, 10:37 am, Chris Stankevitz <chrisstankev...@gmail.com>
wrote:
> Hello,
>
> I would like to remove the "dynamic_cast" and "if" statements from the
> code below.  I believe some people would describe this as making the
> code "polymorphic".  Can you recommend a way to do this without
> modifying the original classes in "Library A"?
>
> My intention is to create an "XML Writer" class(es) for shapes without
> putting "XML code" in the base classes.  I plan to use a similar
> pattern for drawing and other tasks my application has to perform on
> Shape objects.

The adapted visitor pattern that people have shown here is one good
way to solve this. Another you could consider though is a multi-
dispatch mechanism separate from the two parts. This would retain the
dynamic_cast stuff, but you can use TMP to build that. Review _Modern
C++ Design_ by Alexandrescu.

Basically, something like so:

template < typename Seq, typename Enable = void >
struct dispatcher
{
typedef typename front<Seq>::type my_type;
typedef typename pop_front<Seq>::type next_seq;

static void write(Shape const& sh, Writer & wr)
{
if (my_type const* csh = dynamic_cast<my_type const*>(&sh))
wr.write(*csh);
else
dispatcher<next_seq>::write(sh);
}
}

template < typename Seq >
struct dispatcher<Seq, typename enable_if<empty<Seq> >::type>
{
static void write(Shape const&, Writer &) { throw
std::runtime_error("Unknown shape type."); }
};

void write_shape(Shape const& sh, Writer & w)
{
typedef vector<Square, Circle> shape_seq;
dispatcher<shape_seq>::write(sh,w);
}

Chris Stankevitz

unread,
Nov 18, 2011, 8:27:19 PM11/18/11
to
Goran <goran.pu...@gmail.com> wrote:
> Visitor requires changing Shape and the rest of Lib A, which OP said
> didn't want elsethread.

On Nov 18, 8:56 am, Tobias Müller <trop...@bluewin.ch> wrote:
> However, when reading the rest of the thread I had the
> impression that what the OP really wanted is not to leave Lib A unchanged,
> but rather not to include any XML specific features.

You are both correct. At first I said "without modifying LibA" to
discourage "just put the XML specific feature in LibA" (turned out
this didn't discourage the first few replies). Later, as it became
clear I did not pose my question well, my question evolved. As Tobi
picked up, I did not want XML specific features in Library A but was
willing to make changes to Library A. However, I can imagine
scenarios in which LibA is not alterable -- in such a case Tobi's
solution would not work. I apologize for leading the group to believe
that.

> And a vistior interface is quite abstract and useful for a library like A.
>
> And apparently the OP is happy with that solution.

Yes! Not only was I happy with it, when I saw it I knew immediately
that this was what I was thinking of -- I just didn't have a name for
it or know how to go about it or even know if it was possible with c+
+.

Thank you,

Chris
0 new messages