471,612 Members | 2,203 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,612 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 1697

"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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Brian Genisio | last post: by
4 posts views Thread by Roberto Sileoni | last post: by
8 posts views Thread by Elf M. Sternberg | last post: by
8 posts views Thread by Robert | last post: by
2 posts views Thread by Kevin Newman | last post: by
7 posts views Thread by jangchoe | last post: by
1 post views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by

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.