• Advertisement
Sign in to follow this  

toString() in C++

This topic is 4309 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I know in Java, every class extends object and therefore inherits the toString() method. As a result, every class/object has a toString() method. Is it similar in C++? Do all classes extend object? Do all classes have a toString() method?

Share this post


Link to post
Share on other sites
Advertisement
However, there's nothing stopping the classes you make inheriting from CObject or something (which you'd define), and then include a toString() method in that.

Share this post


Link to post
Share on other sites
If you really needed the functionality, you could create the universal base-type and derive equivilant objects for each C++ type. Of course, its never going to be as pervasive as Java or C#, but doing this for the basic built-in types + std::string could be done in a long day or over the weekend. Basically you'd Create this universal base class, then derive a class corresponding to each C++ built-in type, say: "class Int : public Object { ... }" with the built-in type as its only member variable and overload all the operators that the built-in has for your derived object. Add a conversion method to the built-in type which simply returns the member (either implicitly or explicitly), and replicate the conversion characteristics between your derived objects as between the built-ins.

Then, everywhere you use 'int' just use 'Int' instead. All the operators should get inlined for the the built-ins if they're coded right, so the only performance overhead should be conversion, the virtual overloads from the base Object class and possibly construction. You could probably even reproduce The behavior of C#'s enum.ToString (not sure if it matches Java, but probably) with a little template and/or MACRO magic.

If you google around, you may be able to find an existing class library for this type of thing.

Share this post


Link to post
Share on other sites
Quote:
Original post by Ravyne
If you really needed the functionality, you could create the universal base-type and derive equivilant objects for each C++ type. Of course, its never going to be as pervasive as Java or C#, but doing this for the basic built-in types + std::string could be done in a long day or over the weekend. Basically you'd Create this universal base class, then derive a class corresponding to each C++ built-in type, say: "class Int : public Object { ... }" with the built-in type as its only member variable and overload all the operators that the built-in has for your derived object. Add a conversion method to the built-in type which simply returns the member (either implicitly or explicitly), and replicate the conversion characteristics between your derived objects as between the built-ins.

Then, everywhere you use 'int' just use 'Int' instead. All the operators should get inlined for the the built-ins if they're coded right, so the only performance overhead should be conversion, the virtual overloads from the base Object class and possibly construction. You could probably even reproduce The behavior of C#'s enum.ToString (not sure if it matches Java, but probably) with a little template and/or MACRO magic.

If you google around, you may be able to find an existing class library for this type of thing.


Well, I don't know C#, but to make this work like java you wouldn't need to redefine the basic types (int, char, double etc) as they are not full class types in Java, and hence don't need toString() or any methods.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
C++ doesn't have toString, but << operator is pretty standard way of getting a string representation of an object.. So to print out some object to console output, just do

cout << object;


If you want the string out of it, use stringstream:

stringstream ss;
ss << object;
string object_as_string = ss.str();


And to define this operator for a new class, do a free function that is something like this:

ostream& operator<<(ostream& out, const MyClass& x) {
out << "(" << x.attribute << " " << x.attribute2 << ")";
return out;
}


It won't directly support inheritance, but nothing stops you from doing it like this:

ostream& operator<<(ostream& out, const MyClass& x) {
out << x.toString();
return out;
}


Where toString is a virtual toString-method for the class hierarchy whose parent class is MyClass.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement