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:
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.Code:Str& Obj::toString(Str &temp){ temp = "data goes here"; return temp; }
I would really enjoy obtaining this implementation:
Any better ideas?Code:myLoggingSystem(Str()<<"This object contains: "<<myObj.toString());



LinkBack URL
About LinkBacks



