
January 11th, 2006, 10:15 AM
| | | Help with std::string
Hi all,
I want to use std::string instead of char * 's as suggested
at many places in this newsgroup as well as else where, but I am
confused with the way to use it.
For example I have a library built over top of another library which is
C API and my api is in C++. Thus any time I need to call any function
of this C-API, I need to pass the char * while I store them as string
as data member of my classes (see code below). To do so, I have to call
string.c_str() function, but
Q. Is it safe to do so, because I might delete the data in my class
while the C-API might be storing the pointer to that or the c-api might
delete the pointer while I still have reference to the string?
Q. Shall I pass the string into the constructor as reference or shall
I pass as value?
MYCPPAPI(const string & data) or MYCPPAPI(const string data)
I just need to store the passed value in my class MyCPPAPI
If I pass by reference then what are the precautions that need to be
taken?
Q. What precautions should be taken to return the string as reference?
Q. Shall I store the string data member as pointer or just as an
object?
i.e
private:
string data; //or string * data //or const string
data
constructor as
MYCPPAPI(const string & data):_data(data) or
MYCPPAPI(const string & data){
_data = data;
}
or
MYCPPAPI(const string & data){
_data = new string(data); //If _data is string *
} or
///////////////////////////////////// CODE
//////////////////////////////////////////////////////
extern void c_function_call(char * data);
class MyCPPAPI
{
public:
MYCPPAPI(const string & data)
{
this->_data = data;
}
void callCAPIFunction()
{
c_function_call(_data.c_str());
}
private:
string _data;
};
//////////////////////////////////////////////////////////////////////////////////////////////////////
Thanks,
Divick | 
January 11th, 2006, 01:35 PM
| | | Re: Help with std::string
Divick wrote:
[color=blue]
> Q. Is it safe to do so, because I might delete the data in my class
> while the C-API might be storing the pointer to that or the c-api might
> delete the pointer while I still have reference to the string?[/color]
No. The pointer may be invaildated as soon as you modify or destroy the
string. And you must never call delete or free on that pointer.
[color=blue]
> Q. Shall I pass the string into the constructor as reference or shall
> I pass as value?
> MYCPPAPI(const string & data) or MYCPPAPI(const string data)
> I just need to store the passed value in my class MyCPPAPI[/color]
Pass it as reference.
[color=blue]
> If I pass by reference then what are the precautions that need to be
> taken?[/color]
None.
[color=blue]
> Q. What precautions should be taken to return the string as reference?[/color]
None.
[color=blue]
> Q. Shall I store the string data member as pointer or just as an
> object?[/color]
As an object. Don't use a pointer unless you need to.
[color=blue]
> i.e
> private:
> string data; //or string * data //or const string
> data[/color]
What would be the advantage of using a pointer?
[color=blue]
> constructor as
> MYCPPAPI(const string & data):_data(data)[/color]
This one. But you need to add a constructor body.
[color=blue]
> or
> MYCPPAPI(const string & data){
> _data = data;
> }
> or
> MYCPPAPI(const string & data){
> _data = new string(data); //If _data is string *
> } or
>
> ///////////////////////////////////// CODE
> //////////////////////////////////////////////////////
> extern void c_function_call(char * data);
> class MyCPPAPI
> {
> public:
> MYCPPAPI(const string & data)
> {
> this->_data = data;
> }
> void callCAPIFunction()
> {
> c_function_call(_data.c_str());
> }
> private:
> string _data;
> };[/color] | 
January 11th, 2006, 08:05 PM
| | | Re: Help with std::string
On 11 Jan 2006 02:01:06 -0800 in comp.lang.c++, "Divick"
<divick.kishore@gmail.com> wrote,[color=blue]
>Q. Is it safe to do so, because I might delete the data in my class
>while the C-API might be storing the pointer to that or the c-api might
>delete the pointer while I still have reference to the string?[/color]
If the C API stores a pointer to any of your data, that is a
peculiar requirement of that API; it is likely to be awkward for any
number of reasons, and it should be carefully documented how long
the API needs the data to persist, etc..
If it is going to delete a pointer you pass to it, that was not
'new'ed by the API in the first place, then that is a peculiar
requirement of that API and will cause awkwardness, will require
careful documentation etc..
None of that is any different with regard to strings than anything
else.
[color=blue]
>Q. Shall I pass the string into the constructor as reference or shall
>I pass as value?[/color]
Pass by value if you need the parameter to be a COPY of the
argument. Pass by reference if you do not want that copying.
Since you will rarely want a string argument to be copied,
most often the parameter should be a const reference.
[color=blue]
>Q. What precautions should be taken to return the string as reference?[/color]
Never return a reference to a local auto string that disappears
before the (now invalid) return value can be used. Never return a
reference to a dynamically allocated string that the caller will
subsequently have to convert to a pointer in order to delete it.
Never return a reference to a static string that will prevent your
function from being used by multiple threads or for multiple
purposes concurrently without them interfering with each other.
So it comes down to, return strings by value. There are probably
exceptions to this rule.
[color=blue]
>Q. Shall I store the string data member as pointer or just as an
>object?[/color]
Ordinary member. Do not introduce an extra pointer unless you have
a very specific reason to do so. | 
January 11th, 2006, 09:15 PM
| | | Re: Help with std::string
>Pass by value if you need the parameter to be a COPY of the[color=blue]
>argument. Pass by reference if you do not want that copying.
>Since you will rarely want a string argument to be copied,
>most often the parameter should be a const reference.[/color]
Anyway, why would someone prefer to pass a string by value rather than
by const reference? Never! | 
January 11th, 2006, 09:55 PM
| | | Re: Help with std::string
On 11 Jan 2006 13:01:16 -0800 in comp.lang.c++, "Jeff Lefebvre"
<jeff.lefebvre@gmail.com> wrote,[color=blue][color=green]
>>Pass by value if you need the parameter to be a COPY of the
>>argument. Pass by reference if you do not want that copying.
>>Since you will rarely want a string argument to be copied,
>>most often the parameter should be a const reference.[/color]
>
>Anyway, why would someone prefer to pass a string by value rather than
>by const reference? Never![/color]
Example:
string operator+(string left, const string& right)
{
return left += right;
} | 
January 11th, 2006, 09:55 PM
| | | Re: Help with std::string
Jeff Lefebvre wrote:
[color=blue]
> Anyway, why would someone prefer to pass a string by value rather than
> by const reference? Never![/color]
To avoid explicit copy and have another variable name in scope.
--
Salu2 |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over network members.
|