java: How can I do dynamic casting of a variable from one type to another?

Yes it is possible using Reflection Object something = “something”; String theType = “java.lang.String”; Class<?> theClass = Class.forName(theType); Object obj = theClass.cast(something); but that doesn’t make much sense since the resulting object must be saved in a variable of Object type. If you need the variable be of a given class, you can just cast … Read more

dynamic_cast and static_cast in C++

Here’s a rundown on static_cast<> and dynamic_cast<> specifically as they pertain to pointers. This is just a 101-level rundown, it does not cover all the intricacies. static_cast< Type* >(ptr) This takes the pointer in ptr and tries to safely cast it to a pointer of type Type*. This cast is done at compile time. It … Read more