472,960 Members | 1,924 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,960 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 1729

"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: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.