473,327 Members | 1,919 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

prototype for toString() method

I have a class containing 5-6 member data. I want to provide a toString() method
as a part of this class to help the users to do tracing.

const std::string& toString()
{
std::string objectData ;
objectData = "memberData1=" + memberData1 ;
objectData += " memberData2=" + memberData2 ;
objectData += " memberData3=" + memberData3 ;
objectData += " memberData4=" + memberData4 ;
objectData += " memberData5=" + memberData5 ;
return objectData ;
}

Is the function signature Ok? Or, do you suggest to make it as
"std::string toString()" ? If yes, why?
Do you suggest any improvements in the above code?
Jul 22 '05 #1
2 1736

"qazmlp" <qa********@rediffmail.com> skrev i en meddelelse
news:db**************************@posting.google.c om...
I have a class containing 5-6 member data. I want to provide a toString() method as a part of this class to help the users to do tracing.

const std::string& toString()
{
std::string objectData ;
objectData = "memberData1=" + memberData1 ;
objectData += " memberData2=" + memberData2 ;
objectData += " memberData3=" + memberData3 ;
objectData += " memberData4=" + memberData4 ;
objectData += " memberData5=" + memberData5 ;
return objectData ;
}

Is the function signature Ok? Or, do you suggest to make it as
"std::string toString()" ? If yes, why?
Do you suggest any improvements in the above code?


This signature is not ok. You are returning a reference to a local variable.
On return from "toString" this variable does not exist anymore. Thus you
must return a std::string instead.
A still better solution would be to support streaming your data instead.
This is the normal way to provide conversion to string in C++ and would
enable you to use e.g. boost::lexical_cast when all you want is the string.

/Peter


Jul 22 '05 #2
qazmlp wrote:
I have a class containing 5-6 member data. I want to provide a
toString() method as a part of this class to help the users to do
tracing.

const std::string& toString()
{
std::string objectData ;
objectData = "memberData1=" + memberData1 ;
objectData += " memberData2=" + memberData2 ;
objectData += " memberData3=" + memberData3 ;
objectData += " memberData4=" + memberData4 ;
objectData += " memberData5=" + memberData5 ;
return objectData ;
}

Is the function signature Ok?
No.
Or, do you suggest to make it as "std::string toString()" ?
Yes.
If yes, why?
Beause otherwise, your program will probably crash. _Never_ return
references to local variables. They get destroyed before they can be
accessed by the caller.
Do you suggest any improvements in the above code?


If all those memberDataX variables are strings, it should be ok. If not,
you may want to use a stringstream.

Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Brian Genisio | last post by:
Hi all, Does anyone know of a way in IE to determine the prototype name of an object? For instance, in Mozilla, I can say: junk = document.getElementById("myID"); alert(junk.toString());
4
by: Roberto Sileoni | last post by:
While i was trying to understand prototype behavior, i wrote this sample code: function MyString() { } MyString.prototype = new String() var a=new MyString("aaaaa"); alert(a.valueOf());
8
by: Elf M. Sternberg | last post by:
One of the complaints about prototype.js (google for it if you're not familiar with it) is that it's poorly documented. I have this inkling that the key to understanding prototype.js is in the...
8
by: Robert | last post by:
Hi, I can use "with" like this: function MyObject(message) { this.message = message; } function _MyObject_speak() {
2
by: stephane | last post by:
Hi all, What I am trying to achieve is an 'inherits' method similar to Douglas Crockford's (http://www.crockford.com/javascript/inheritance.html) but that can enable access to the superclass'...
2
by: Kevin Newman | last post by:
Hello, I noticed that the JavaScript library for JSON posted on json.org (http://www.json.org/json.js) is modifying Object.prototype (adding a method - toJSONString). I thought this was...
8
by: Csaba Gabor | last post by:
I wrote a .repeat(n) function for strings which seemed to work fine: String.prototype.repeat = function(n) { // repeats the string n times if (n<1) return ""; if (n<2) return this; for (var...
7
by: jangchoe | last post by:
I've heard that JavaScript is a prototype-based language instead of a class based one. I'm interested in learning the prototype-based paradigm of OO programming, but I am unsure of the best way to...
3
by: Robert | last post by:
Hi, I thought I pretty much understood the whole prototype chain stuff, but now I stumbled upon a difference between IE and Firefox, that is totally confusing me. An example.......
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.