Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
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.
Object * p = createMyObject();
String * s = dynamic_cast<String *>(p);
if(s)
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.
–
–
In C++ there is no base object class, so in general there is no way of doing this. You can however do it for specific hierarchies:
struct A {
virtual ~A() {}
struct B : public A {
A * p = Something(); // Something() may return an A * or a B *
B & b = dynamic_cast <B&>(*p);
The dynamic cast will throw an exception if p does not point at something that can safely be converted to a B reference.
–
–
–
–
–
In C# the as
keyword converts a value to another type. There is code that supports this conversion (probably in the C# runtime). This is not a cast.
For the most part you can't use dynamic_cast
or any other kind of cast in C++ to accomplish this, because casts and conversions are not the same thing. I say 'for the most part' because some types can be converted using static_cast
, to convert an int
to a float
but this is still a conversion, not a cast. Also, if you have introduced a type system where everything is derived from an Object
-like base class that has this conversion functionality, you might be able to construct a mechanism to support this conversion using dynamic_cast
, but you would have had to write this mechanism and this does not seem to be what you're trying to do.
There is nothing built-in to C++ which will do this conversion for you; in other words, there is no C++ equivalent to the C# as
keyword.
If you want to perform this conversion, you can often use streams:
#include <sstream>
#include <string>
#include <iostream>
using namespace std;
int main()
float f = 42.0f;
stringstream ss;
ss << f;
string s = ss.str();
cout << "Float: " << f << ", String '" << s << "'";
return 0;
<sstream>
is part of the C++ Standard, so in this regard you might consider it to be 'in the language'.
Using streams to do this conversion can be rather clumsy. Boost offers lexical_cast
which can be used to perform these simple conversions with less code:
#include <sstream>
#include <iostream>
#include <boost\lexical_cast.hpp>
using namespace std;
int main()
string s = "42";
float f = boost::lexical_cast<float>(s);
cout << "Float: " << f << ", String '" << s << "'";
return 0;
–
–
–
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.