In C# you can use as to convert a type or get null:
Object o = Whatever();
String s = o as String;
Is there a similar easy way to achieve this in C++?
I’m using Visual Studio 2010, if that’s important.
[Update]: Remember, there is a very important difference between casting and using as. Casting (at least in C#) will throw an exception if the type does not match:
Object o = null;
String s = (String)o; // Will crash.
In C++, this would be a dynamic_cast, if you had a hierarchy where Object is the parent and String is the child.
Dynamic casting a pointer will return a pointer to the object if the cast is possible, or a null pointer if not.
Also, dynamic casting a reference will return a reference to the object if the cast is possible, or throw an exception.