Thread: [Dev-C++] How would you write a method returning a string ?
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Hiroyuki Y. <yam...@gm...> - 2007-07-21 15:22:50
|
Hi, I'm wondering how you would write a method returning a string. In C, we usually return char * which malloced in the function or returning char * which is static in the function. (We should free the char * in the first case.) In C++, we have std::string, so i'm not sure what to do with it to return string. What I'm trying to do as following. 1. use char * as a return value just like C 2. use std::string as a return value (copying the string object) 3. use std::string reference as one of the arguments (return value is bool or something.) 4. use std::string reference as a return value There should be more ways, but I'm not familier with those (teach me those please) and I don't know what's the best. I'm going to use method 2 because it's simple and copying string object is not a much cost I think. (It's said that string object is 7 times as big as the pointer size(4bytes) at the biggest in Effective STL) Please let me know how you would write to return string. Thanks, HIroyuki Yamada |
|
From: Per W. <pw...@ia...> - 2007-07-21 18:16:19
|
Yes, the stdd:string type is bigger than a pointer, since it contains more than a pointer. It also contains (at least) a length and a reference counter. Assigning one std::string to another does not normally require a copy of the text data. A normal implementation of std::string uses a reference-counted buffer for the text data, and a new std::string will just point to the same text. /pwm On Sun, 22 Jul 2007, Hiroyuki Yamada wrote: > Hi, > > I'm wondering how you would write a method returning a string. > > In C, we usually return char * which malloced in the function or > returning char * which is static in the function. > (We should free the char * in the first case.) > > In C++, > we have std::string, so i'm not sure what to do with it to return string. > > What I'm trying to do as following. > > 1. use char * as a return value just like C > 2. use std::string as a return value (copying the string object) > 3. use std::string reference as one of the arguments (return value is > bool or something.) > 4. use std::string reference as a return value > > There should be more ways, but I'm not familier with those (teach me > those please) > and I don't know what's the best. > I'm going to use method 2 because > it's simple and copying string object is not a much cost I think. > (It's said that string object is 7 times as big as the pointer > size(4bytes) at the biggest in Effective STL) > > Please let me know how you would write to return string. > > > Thanks, > > HIroyuki Yamada > > ------------------------------------------------------------------------- > This SF.net email is sponsored by: Microsoft > Defy all challenges. Microsoft(R) Visual Studio 2005. > http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/ > _______________________________________________ > Dev-cpp-users mailing list > Dev...@li... > TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > |
|
From: Jamiil A. <jal...@gm...> - 2007-07-22 11:00:33
|
----- Original Message -----
From: "Per Westermark" <pw...@ia...>
To: "Hiroyuki Yamada" <yam...@gm...>
Cc: <dev...@li...>
Sent: Saturday, July 21, 2007 11:16 AM
Subject: Re: [Dev-C++] How would you write a method returning a string ?
> Yes, the stdd:string type is bigger than a pointer, since it contains more
> than a pointer. It also contains (at least) a length and a reference
> counter.
std::string& NameSpace::ClassName::MethodName(const char* var){return
someString = var;}
or
const char* NameSpace::ClassName::MethodName(std::string& str){return
str.c_str();}
>
> Assigning one std::string to another does not normally require a copy of
> the text data. A normal implementation of std::string uses a
> reference-counted buffer for the text data, and a new std::string will
> just point to the same text.
>
> /pwm
>
> On Sun, 22 Jul 2007, Hiroyuki Yamada wrote:
>
>> Hi,
>>
>> I'm wondering how you would write a method returning a string.
>>
>> In C, we usually return char * which malloced in the function or
>> returning char * which is static in the function.
>> (We should free the char * in the first case.)
>>
>> In C++,
>> we have std::string, so i'm not sure what to do with it to return string.
>>
>> What I'm trying to do as following.
>>
>> 1. use char * as a return value just like C
>> 2. use std::string as a return value (copying the string object)
>> 3. use std::string reference as one of the arguments (return value is
>> bool or something.)
>> 4. use std::string reference as a return value
>>
>> There should be more ways, but I'm not familier with those (teach me
>> those please)
>> and I don't know what's the best.
>> I'm going to use method 2 because
>> it's simple and copying string object is not a much cost I think.
>> (It's said that string object is 7 times as big as the pointer
>> size(4bytes) at the biggest in Effective STL)
>>
>> Please let me know how you would write to return string.
>>
>>
>> Thanks,
>>
>> HIroyuki Yamada
>>
>> -------------------------------------------------------------------------
>> This SF.net email is sponsored by: Microsoft
>> Defy all challenges. Microsoft(R) Visual Studio 2005.
>> http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
>> _______________________________________________
>> Dev-cpp-users mailing list
>> Dev...@li...
>> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
>> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>>
>
>
> -------------------------------------------------------------------------
> This SF.net email is sponsored by: Splunk Inc.
> Still grepping through log files to find problems? Stop.
> Now Search log events and configuration files using AJAX and a browser.
> Download your FREE copy of Splunk now >> http://get.splunk.com/
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
|
|
From: matheus r. <mfr...@gm...> - 2007-07-22 15:45:42
|
> std::string& NameSpace::ClassName::MethodName(const char* var){return
> someString = var;}
Not a good idea if somestring is a function variable. It will go out
of scope and be destroyed, returning a ref to it will yield undefined
results.
|
|
From: Jamiil A. <jal...@gm...> - 2007-07-22 23:56:13
|
And how would you suggest one should write a method that receives a 'const
char*' and returns a 'std::string&'?
----- Original Message -----
From: "matheus ribeiro" <mfr...@gm...>
To: <dev...@li...>
Sent: Sunday, July 22, 2007 8:45 AM
Subject: Re: [Dev-C++] How would you write a method returning a string ?
>> std::string& NameSpace::ClassName::MethodName(const char* var){return
>> someString = var;}
>
>
> Not a good idea if somestring is a function variable. It will go out
> of scope and be destroyed, returning a ref to it will yield undefined
> results.
>
> -------------------------------------------------------------------------
> This SF.net email is sponsored by: Splunk Inc.
> Still grepping through log files to find problems? Stop.
> Now Search log events and configuration files using AJAX and a browser.
> Download your FREE copy of Splunk now >> http://get.splunk.com/
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
|
|
From: matheus r. <mfr...@gm...> - 2007-07-23 00:26:00
|
The first question to ask is: why would one need that? C++ strings
were made thinking of them as a base value, like int.
Id rather write a function with
const std::string somefunction(const char *val)
prototype.
2007/7/22, Jamiil Abduqadir <jal...@gm...>:
> And how would you suggest one should write a method that receives a 'const
> char*' and returns a 'std::string&'?
>
>
>
> ----- Original Message -----
> From: "matheus ribeiro" <mfr...@gm...>
> To: <dev...@li...>
> Sent: Sunday, July 22, 2007 8:45 AM
> Subject: Re: [Dev-C++] How would you write a method returning a string ?
>
>
> >> std::string& NameSpace::ClassName::MethodName(const char* var){return
> >> someString = var;}
> >
> >
> > Not a good idea if somestring is a function variable. It will go out
> > of scope and be destroyed, returning a ref to it will yield undefined
> > results.
> >
> > -------------------------------------------------------------------------
> > This SF.net email is sponsored by: Splunk Inc.
> > Still grepping through log files to find problems? Stop.
> > Now Search log events and configuration files using AJAX and a browser.
> > Download your FREE copy of Splunk now >> http://get.splunk.com/
> > _______________________________________________
> > Dev-cpp-users mailing list
> > Dev...@li...
> > TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
>
> -------------------------------------------------------------------------
> This SF.net email is sponsored by: Splunk Inc.
> Still grepping through log files to find problems? Stop.
> Now Search log events and configuration files using AJAX and a browser.
> Download your FREE copy of Splunk now >> http://get.splunk.com/
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|
|
From: Jamiil A. <jal...@gm...> - 2007-07-23 00:35:32
|
I have many classes that manipulate the string and then returns it either as
a 'const char*', 'std::string', 'int', and so on and so forth, the method is
just part of the class, so instead of writing a stand alone function, it is
better to pack it up within the class, after all... that is the nature of
Object Oriented Programming.
----- Original Message -----
From: "matheus ribeiro" <mfr...@gm...>
Cc: <dev...@li...>
Sent: Sunday, July 22, 2007 5:25 PM
Subject: Re: [Dev-C++] How would you write a method returning a string ?
> The first question to ask is: why would one need that? C++ strings
> were made thinking of them as a base value, like int.
>
> Id rather write a function with
>
> const std::string somefunction(const char *val)
>
> prototype.
>
> 2007/7/22, Jamiil Abduqadir <jal...@gm...>:
>> And how would you suggest one should write a method that receives a
>> 'const
>> char*' and returns a 'std::string&'?
>>
>>
>>
>> ----- Original Message -----
>> From: "matheus ribeiro" <mfr...@gm...>
>> To: <dev...@li...>
>> Sent: Sunday, July 22, 2007 8:45 AM
>> Subject: Re: [Dev-C++] How would you write a method returning a string ?
>>
>>
>> >> std::string& NameSpace::ClassName::MethodName(const char* var){return
>> >> someString = var;}
>> >
>> >
>> > Not a good idea if somestring is a function variable. It will go out
>> > of scope and be destroyed, returning a ref to it will yield undefined
>> > results.
>> >
>> > -------------------------------------------------------------------------
>> > This SF.net email is sponsored by: Splunk Inc.
>> > Still grepping through log files to find problems? Stop.
>> > Now Search log events and configuration files using AJAX and a browser.
>> > Download your FREE copy of Splunk now >> http://get.splunk.com/
>> > _______________________________________________
>> > Dev-cpp-users mailing list
>> > Dev...@li...
>> > TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
>> > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>>
>>
>> -------------------------------------------------------------------------
>> This SF.net email is sponsored by: Splunk Inc.
>> Still grepping through log files to find problems? Stop.
>> Now Search log events and configuration files using AJAX and a browser.
>> Download your FREE copy of Splunk now >> http://get.splunk.com/
>> _______________________________________________
>> Dev-cpp-users mailing list
>> Dev...@li...
>> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
>> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>>
>
> -------------------------------------------------------------------------
> This SF.net email is sponsored by: Splunk Inc.
> Still grepping through log files to find problems? Stop.
> Now Search log events and configuration files using AJAX and a browser.
> Download your FREE copy of Splunk now >> http://get.splunk.com/
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
|
|
From: matheus r. <mfr...@gm...> - 2007-07-23 00:41:32
|
> I have many classes that manipulate the string and then returns it either as > a 'const char*', 'std::string', 'int', and so on and so forth, the method is > just part of the class, so instead of writing a stand alone function, it is > better to pack it up within the class, after all... that is the nature of > Object Oriented Programming. As long as the reference you return exists, no problem. But suppose the object of the class mentioned is destroyed, your string reference will be dangling, yielding undefined results. Of course, as long as you know what youre doing, youre fine. But I wouldnt advice people, specially novices, doing that sort of function. Just MHO. |
|
From: Adam J. <aj...@i-...> - 2007-07-23 01:52:59
|
>>Of course, as long as you know what youre doing, youre fine. But I >>wouldnt advice people, specially novices, doing that sort of function. I would definitely agree with that statement. You really need a good reason to do that. What you are trying to do is potentially dangerous. I would also document this in comments what you are doing, just in case other programmers use your class. There are safer ways to accomplish what you are trying to do..... :) -Adam Jones -----Original Message----- From: dev...@li... [mailto:dev...@li...] On Behalf Of matheus ribeiro Sent: Sunday, July 22, 2007 8:41 PM Cc: dev...@li... Subject: Re: [Dev-C++] How would you write a method returning a string ? > I have many classes that manipulate the string and then returns it either as > a 'const char*', 'std::string', 'int', and so on and so forth, the method is > just part of the class, so instead of writing a stand alone function, it is > better to pack it up within the class, after all... that is the nature of > Object Oriented Programming. As long as the reference you return exists, no problem. But suppose the object of the class mentioned is destroyed, your string reference will be dangling, yielding undefined results. Of course, as long as you know what youre doing, youre fine. But I wouldnt advice people, specially novices, doing that sort of function. Just MHO. ------------------------------------------------------------------------- This SF.net email is sponsored by: Splunk Inc. Still grepping through log files to find problems? Stop. Now Search log events and configuration files using AJAX and a browser. Download your FREE copy of Splunk now >> http://get.splunk.com/ _______________________________________________ Dev-cpp-users mailing list Dev...@li... TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm https://lists.sourceforge.net/lists/listinfo/dev-cpp-users |
|
From: Adam J. <aj...@i-...> - 2007-07-23 02:22:24
|
>> The first question to ask is: why would one need that? C++ strings
>> were made thinking of them as a base value, like int.
>>
>> Id rather write a function with
>>
>> const std::string somefunction(const char *val)
>>
>> prototype.
I agree with this prototype function. This is much safer.
>> I have many classes that manipulate the string and then returns it either
as
>> a 'const char*', 'std::string', 'int', and so on and so forth, the method
is
>> just part of the class, so instead of writing a stand alone function, it
is
>> better to pack it up within the class, after all... that is the nature of
>> Object Oriented Programming.
First of all you don't need a standalone function to get a const char * from
a std::string. Try std::string.c_str() it returns a const char*. Second,
that is not what Object Oriented Programming is all about. Object Oriented
programming is about Inheritance, Data hiding, and polymorphism. Being able
to create a base class and inherit from that class to create other objects,
and to be able to hide the implementation and data, along with using
polymorphism. Is what OOP is all about. If you have created a class that
isn't being use for inheritance, data hiding, and polymorphism, then all you
have done is created a class with a bunch of functions in them. If that is
the case, then that is not OOP.
-Adam Jones
-----Original Message-----
From: dev...@li...
[mailto:dev...@li...] On Behalf Of Jamiil
Abduqadir
Sent: Sunday, July 22, 2007 11:39 PM
To: matheus ribeiro
Cc: dev...@li...
Subject: Re: [Dev-C++] How would you write a method returning a string ?
I have many classes that manipulate the string and then returns it either as
a 'const char*', 'std::string', 'int', and so on and so forth, the method is
just part of the class, so instead of writing a stand alone function, it is
better to pack it up within the class, after all... that is the nature of
Object Oriented Programming.
----- Original Message -----
From: "matheus ribeiro" <mfr...@gm...>
Cc: <dev...@li...>
Sent: Sunday, July 22, 2007 5:25 PM
Subject: Re: [Dev-C++] How would you write a method returning a string ?
> The first question to ask is: why would one need that? C++ strings
> were made thinking of them as a base value, like int.
>
> Id rather write a function with
>
> const std::string somefunction(const char *val)
>
> prototype.
>
> 2007/7/22, Jamiil Abduqadir <jal...@gm...>:
>> And how would you suggest one should write a method that receives a
>> 'const
>> char*' and returns a 'std::string&'?
>>
>>
>>
>> ----- Original Message -----
>> From: "matheus ribeiro" <mfr...@gm...>
>> To: <dev...@li...>
>> Sent: Sunday, July 22, 2007 8:45 AM
>> Subject: Re: [Dev-C++] How would you write a method returning a string ?
>>
>>
>> >> std::string& NameSpace::ClassName::MethodName(const char* var){return
>> >> someString = var;}
>> >
>> >
>> > Not a good idea if somestring is a function variable. It will go out
>> > of scope and be destroyed, returning a ref to it will yield undefined
>> > results.
>> >
>> >
-------------------------------------------------------------------------
>> > This SF.net email is sponsored by: Splunk Inc.
>> > Still grepping through log files to find problems? Stop.
>> > Now Search log events and configuration files using AJAX and a browser.
>> > Download your FREE copy of Splunk now >> http://get.splunk.com/
>> > _______________________________________________
>> > Dev-cpp-users mailing list
>> > Dev...@li...
>> > TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
>> > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>>
>>
>> -------------------------------------------------------------------------
>> This SF.net email is sponsored by: Splunk Inc.
>> Still grepping through log files to find problems? Stop.
>> Now Search log events and configuration files using AJAX and a browser.
>> Download your FREE copy of Splunk now >> http://get.splunk.com/
>> _______________________________________________
>> Dev-cpp-users mailing list
>> Dev...@li...
>> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
>> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>>
>
> -------------------------------------------------------------------------
> This SF.net email is sponsored by: Splunk Inc.
> Still grepping through log files to find problems? Stop.
> Now Search log events and configuration files using AJAX and a browser.
> Download your FREE copy of Splunk now >> http://get.splunk.com/
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems? Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >> http://get.splunk.com/
_______________________________________________
Dev-cpp-users mailing list
Dev...@li...
TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
|