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