std::string or char* as parameters to function in C++?

Started by
8 comments, last by MENTAL 18 years, 8 months ago
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
/roqhttp://roq.blogspot.com
Advertisement
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:
    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).
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
Quote:Original post by JohnBolton
    void 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?
Anything posted is personal opinion which does not in anyway reflect or represent my employer. Any code and opinion is expressed “as is” and used at your own risk – it does not constitute a legal relationship of any kind.
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 :)
Quote:Original post by SiCrane
Quote:Original post by JohnBolton
    void 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?
Anything posted is personal opinion which does not in anyway reflect or represent my employer. Any code and opinion is expressed “as is” and used at your own risk – it does not constitute a legal relationship of any kind.
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 paulecoyote
Quote:Original post by SiCrane
Quote:Original post by JohnBolton
    void 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?


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?
Anything posted is personal opinion which does not in anyway reflect or represent my employer. Any code and opinion is expressed “as is” and used at your own risk – it does not constitute a legal relationship of any kind.
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