Re: [Dev-C++] How would you write a method returning a string ?
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
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
|