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