I am making various objects in my code that contain a toString() method. This method was originally intended to take in nothing and return a string object. Unfortunately, the obvious downfall occurs from the code below:
Code:
Str Obj::toString()
{
Str temp;
temp = "data goes here";
return temp;
}
This would be a very nice way to implement the code, and I do believe this implementation works in Java. However, either it works or not in Java, it does for me in C++. Many people will most likely say to use pointers, but I would rather stay away from that if possible. Instead, my current implementation is listed below:
Code:
Str& Obj::toString(Str &temp){
temp = "data goes here";
return temp;
}
where a temporary variable is passed in from the same scope, therefore it is also released at the same scope it's finished.
I would really enjoy obtaining this implementation:
Code:
myLoggingSystem(Str()<<"This object contains: "<<myObj.toString());
Any better ideas?