Help with std::string

D

Divick

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
 
R

Rolf Magnus

Divick said:
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?

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.
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

Pass it as reference.
If I pass by reference then what are the precautions that need to be
taken?
None.

Q. What precautions should be taken to return the string as reference?
None.

Q. Shall I store the string data member as pointer or just as an
object?

As an object. Don't use a pointer unless you need to.
i.e
private:
string data; //or string * data //or const string
data

What would be the advantage of using a pointer?
constructor as
MYCPPAPI(const string & data):_data(data)

This one. But you need to add a constructor body.
 
D

David Harmon

On 11 Jan 2006 02:01:06 -0800 in comp.lang.c++, "Divick"
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?

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.
Q. Shall I pass the string into the constructor as reference or shall
I pass as value?

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.
Q. What precautions should be taken to return the string as reference?

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.
Q. Shall I store the string data member as pointer or just as an
object?

Ordinary member. Do not introduce an extra pointer unless you have
a very specific reason to do so.
 
J

Jeff Lefebvre

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.

Anyway, why would someone prefer to pass a string by value rather than
by const reference? Never!
 
D

David Harmon

On 11 Jan 2006 13:01:16 -0800 in comp.lang.c++, "Jeff Lefebvre"
Anyway, why would someone prefer to pass a string by value rather than
by const reference? Never!

Example:

string operator+(string left, const string& right)
{
return left += right;
}
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

Jeff said:
Anyway, why would someone prefer to pass a string by value rather than
by const reference? Never!

To avoid explicit copy and have another variable name in scope.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Help with Loop 0
Can't solve problems! please Help 0
Need help with this script 4
problem with std::forward_as_tuple in MIL 1
Chatbot 0
Help with pointers 1
Help with code 0
[HELP] Add-on - Twitch chat input 0

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top