* jimmy:
Please forgive me if this is elementary. I can't seem to find the
solution anywhere.
// foo() is only declared/defined in Derived
Base* base = new Derived();
((Derived*)base)->foo();
Given that I would like to use a Base*; is casting the only solution?
Preferably 'foo' should be a virtual function declared in 'Base' and
overridden in 'Derived'.
Failing that -- if 'foo' is not meaningful in 'Base', but you still
need to handle heterogenous collection of 'Base' pointers -- you should
use the visitor pattern.
Finally, if that isn't an option either, then you have a fundamental design
error and should redesign.
A static_cast can technically do the job instead of one of the above, but it
means you're doing assembly-language-level programming instead of C++.
As others have mentioned, the cast you're using is _even worse_ than a
static_cast: it's akin to using an assembly language where you _don't
know_ what machine code will be emitted for an instruction, the assembler
choosing whatever it likes the most at the moment. Old C style casts
in C++ generally means (1) complete newbie, (2) Microsoft employee, or (3)
incompetent but non-MS. (There is one thing C style casts can do that C++
casts can't, but that's advanced.) So if for nothing else, then at least
for your own image, avoid C-style casts, completely.