474,052 Members | 1,990 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 = "memberData 1=" + 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::strin g toString()" ? If yes, why?
Do you suggest any improvements in the above code?
Jul 22 '05 #1
2 1763

"qazmlp" <qa********@red iffmail.com> skrev i en meddelelse
news:db******** *************** ***@posting.goo gle.com...
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 = "memberData 1=" + 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::strin g 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 = "memberData 1=" + 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::strin g 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
2214
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
2129
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
3768
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 bind function. The problem with Javascript is that the "this" operator is poorly overloaded and it is often hard to understand in the context of object-oriented javascript So, let's start with the definition:
8
2072
by: Robert | last post by:
Hi, I can use "with" like this: function MyObject(message) { this.message = message; } function _MyObject_speak() {
2
3052
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' priviledged methods also. Do you know if this is possible ? In the following example, I create an ObjectA (variable a), an ObjectB which inherits ObjectA (variable b) and an ObjectC which inherits ObjectA (variable c1). The 'toString ()' method...
2
3782
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 considered bad practice because it can disrupt the use of for in loops on Objects. Am I incorrect? Thanks,
8
1957
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 aStr = ;--n>0;) aStr.push(aStr); return aStr.join(""); }
7
1544
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 use JavaScript as a prototype-based language. I'm stuck between two ways and both methods might be right (or wrong). Method 1: /* create an Animal "object" */ function Animal() {} ;
3
2774
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.... ************************ function MyObject() {
0
10564
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10365
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
12178
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
11624
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
11155
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
10346
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8727
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7894
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6678
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...

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.