C++ Equivalent of Java's Tostring

C++ equivalent of Java's toString?

In C++ you can overload operator<< for ostream and your custom class:

class A {
public:
int i;
};

std::ostream& operator<<(std::ostream &strm, const A &a) {
return strm << "A(" << a.i << ")";
}

This way you can output instances of your class on streams:

A x = ...;
std::cout << x << std::endl;

In case your operator<< wants to print out internals of class A and really needs access to its private and protected members you could also declare it as a friend function:

class A {
private:
friend std::ostream& operator<<(std::ostream&, const A&);
int j;
};

std::ostream& operator<<(std::ostream &strm, const A &a) {
return strm << "A(" << a.j << ")";
}

Java ToString method in C++

Yes, C++11 introduces something similar in std::to_string.

Before that, your safest bet was to use a std::stringstream, fill it with operator <<, and retrieve the string with .str().

C++ toString operator similair to Object.toString

There is no equivalent. Unlike JAVA, not everything in C++ is derived from some (Object) superclass. There is no ::toString() member function as there is no superclass in C++ to begin with. C++ does not support reflection either.

That being said, there is a std::to_string function having 9 different overloads for the built-in types. To achieve the functionality you want, you can overload the output stream operator<< for each of your classes.

toString() method in C++?

You should probably modify the class called MediaDescription like that:

class MediaDescription {
/*code*/
public: /*or anything you need*/
/*code*/
std::wstring toString();
}

Is there an equivalent to Java's ToStringBuilder for C#? What would a good C# version feature?

EDIT: OK, you want to use reflection so you don't have to type property names. I think this will get you what you're after:

// forgive the mangled code; I hate horizontal scrolling
public sealed class ToStringBuilder<T> {
private T _obj;
private Type _objType;
private StringBuilder _innerSb;

public ToStringBuilder(T obj) {
_obj = obj;
_objType = obj.GetType();
_innerSb = new StringBuilder();
}

public ToStringBuilder<T> Append<TProperty>
(Expression<Func<T, TProperty>> expression) {

string propertyName;
if (!TryGetPropertyName(expression, out propertyName))
throw new ArgumentException(
"Expression must be a simple property expression."
);

Func<T, TProperty> func = expression.Compile();

if (_innerSb.Length < 1)
_innerSb.Append(
propertyName + ": " + func(_obj).ToString()
);
else
_innerSb.Append(
", " + propertyName + ": " + func(_obj).ToString()
);

return this;
}

private static bool TryGetPropertyName<TProperty>
(Expression<Func<T, TProperty>> expression, out string propertyName) {

propertyName = default(string);

var propertyExpression = expression.Body as MemberExpression;
if (propertyExpression == null)
return false;

propertyName = propertyExpression.Member.Name;
return true;
}

public override string ToString() {
return _objType.Name + "{" + _innerSb.ToString() + "}";
}
}

Example:

// from within some class with an Id and Name property
public override string ToString() {
return new ToStringBuilder<SomeClass>(this)
.Append(x => x.Id)
.Append(x => x.Name)
.ToString();
}

Behold, the behavior you're after:

class Thing {
public int Id { get; set; }
public string Name { get; set; }

public override string ToString() {
return new ToStringBuilder<Thing>(this)
.Append(t => t.Id)
.Append(t => t.Name)
.ToString()
}
}

void Main() {
var t = new Thing { Id = 10, Name = "Bob" };
Console.WriteLine(t.ToString());
}

Output:

Thing{Id: 10, Name: "Bob"}

toString override in C++

std::ostream & operator<<(std::ostream & Str, Object const & v) { 
// print something from v to str, e.g: Str << v.getX();
return Str;
}

If you write this in a header file, remember to mark the function inline: inline std::ostream & operator<<(... (See the C++ Super-FAQ for why.)

How to print structures in C programming, anything similar to the toString() method of Java in c?

No real magic:

struct Node node = {"Adam", 1, NULL, NULL};
struct Node *nodePtr = &node;

printf("word[%s] count[%d] left[%p] right[%p]\n",
node.word, node.count, node.left, node.right);

printf("word[%s] count[%d] left[%p] right[%p]\n",
nodePtr->word, nodePtr->count, nodePtr->left, nodePtr->right);

C# .ToString(X4) equivalent in Java

I think there isn't are true equivalent.
Java has a Format-class, if you would like to take a look at that.

function:Integer.toHexString((int)chr)

Is here the correct way to do it, so that should be it.



Related Topics



Leave a reply



Submit