std::string or char* as parameters to function in C++?
Is there a preferred way to use strings as parameters? I know that you usually should use std::string wherever possible instead of char*.
If we have the two function proptypes below:
void myfunction(char* mystring);
void myfunction(std::string mystring);
I think that the first version may be as good as the second and perhaps even better since it gives the user of the function the option to either use char* or std::string (with c_str()) when calling the function. If the user is programming with C he cannot use std::string. Internally both functions can use std::string to do string operations.
Does it cost a lot in performance to call std::string::c_str() and to create a std::string from a char*?
/roq
It is not possible to know which way is better performance-wise without knowing how the parameter is used. std::string might be slower because it means an extra indirection, but if you are going to do that indirection anyway, then there is no advantage to char *. If you need the length of the string then std::string will be faster because the length does not have to be computed.
Anyway, the safety, flexibility, and functionality of std::string is generally more important than the few nanoseconds you might save using char *.
BTW, the function parameters would look like this:
std::string::c_str() is probably very fast (just an educated guess) and std::string( char * ) is probably slow (because it may involve memory allocation and copying).
Anyway, the safety, flexibility, and functionality of std::string is generally more important than the few nanoseconds you might save using char *.
BTW, the function parameters would look like this:
void myfunction(char* mystring); void myfunction(std::string & mystring);
Quote:Does it cost a lot in performance to call std::string::c_str() and to create a std::string from a char*?
std::string::c_str() is probably very fast (just an educated guess) and std::string( char * ) is probably slow (because it may involve memory allocation and copying).
Quote:Original post by JohnBoltonvoid myfunction(char* mystring); void myfunction(std::string & mystring);
Or if the string parameters are treated as read only values:
void myfunction(const char * mystring); void myfunction(const std::string & mystring);
that's for setter functions, but out of interest what about getter style functions where a string is the return value?
If at all possible try to use reference argument to fill in a string or return a reference to a string if posible, but try to avoid return a string.
void getSomeThing(string& fill);
string& getSomeThingByRef(); // can change return string
const string& getSomeThingByRefConst(); // can't change return string
instead of:
string getSomeThing(); // could invole 1 or more copies for strings
Lord Bart :)
void getSomeThing(string& fill);
string& getSomeThingByRef(); // can change return string
const string& getSomeThingByRefConst(); // can't change return string
instead of:
string getSomeThing(); // could invole 1 or more copies for strings
Lord Bart :)
Quote:Original post by SiCraneQuote:Original post by JohnBoltonvoid myfunction(char* mystring); void myfunction(std::string & mystring);
Or if the string parameters are treated as read only values:void myfunction(const char * mystring); void myfunction(const std::string & mystring);
resurrecting this thread a little, but would it be a faux pas to just have
void myfunction(const std::string & mystring);
rather then
void myfunction(const char * mystring); void myfunction(const std::string & mystring);
- and just let the implicit conversion of a char* to std::string take place?
Doing that usually isn't a problem. You might get better performance out of having both in some situations, but that's something you can worry about if your profiler tells you there is an issue with the function.
Quote:Original post by paulecoyoteQuote:Original post by SiCraneQuote:Original post by JohnBoltonvoid myfunction(char* mystring); void myfunction(std::string & mystring);
Or if the string parameters are treated as read only values:void myfunction(const char * mystring); void myfunction(const std::string & mystring);
resurrecting this thread a little, but would it be a faux pas to just havevoid myfunction(const std::string & mystring);
rather thenvoid myfunction(const char * mystring); void myfunction(const std::string & mystring);
- and just let the implicit conversion of a char* to std::string take place?
But the original problem was allowing C programmers to use the function (i.e. when std::string wasn't available).
Back to the original poster, either write your library in straight C or go for C++ - DON'T try and make comprimises for both as it will lead to an interface that is cluttered and hard to maintain.
// Good code - no jokes about g_string please ;)void SetString (const std::string &src_string){ g_string = src_string;}const std::string &GetString (){ return g_string;}
If the user needs to use the result of GetString in a legacy library, let him make the call to c_str himself.
MENTAL > I was hijacking a bit - forget about linking with C or dlls or anything, I meant internally. Just letting const char* be implicitly converted to const string& is alright in the land of C++ elite, right?
It certainly is, as it's the exact same method for passing a string litteral to a stl string.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement