Re: How can I remove dynamic_cast and if statements from this code
snippet?
On 11/16/2011 11:14 AM, Chris Stankevitz wrote:
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".
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