Once More About dynamic_cast, a Real Use Case -- Sandor Dargo
While
dynamic_cast
is often discouraged for its impact on readability and reliance on RTTI, there are rare situations where it can be the most practical and safe solution. In this post, we explore one such real-world case: using dynamic_cast
for runtime versioning of plugin interfaces in a way that balances compatibility, safety, and extensibility.
Once More About dynamic_cast, a Real Use Case
by Sandor Dargo
From the article:
I wrote a couple of times about
dynamic_cast
and I discouraged you from using it. In general, it makes code worse in terms of readability. When you get rid ofdynamic_cast
, either via self-discipline or by turning RTTI off, you’ll have to rely on dynamic dispatching and better abstractions.But there might be cases, when it’s not possible or at least it’s not meaningful to remove
dynamic_cast
, here is one, sent by one of you.Versioning with the help of
dynamic_cast
They have an SDK that anyone can implement. As there are new features added every now and then, the API keeps changing. Not surprisingly, the owners of the SDK want to prevent their users’ code from breaking. They achieve this by having different “versioned” interfaces for the same service where a new version inherits from the previous one.
Let’s see a simplified example.