0

I wrote a program a while ago in c language and want to move to c++, I had:

BuiltInCommand(const char* cmd_line);

what is the best way to use string here?

BuiltInCommand(const string cmd_line);

or

BuiltInCommand(const string& cmd_line);
Galik
  • 47,303
  • 4
  • 80
  • 117
  • What do you mean, specifically, by "best"? – Sam Varshavchik Apr 17 '21 at 14:43
  • 4
    That depends on what `BuiltInCommand` does. – Devolus Apr 17 '21 at 14:45
  • @SamVarshavchik strings are moved by value right? so no need to const –  Apr 17 '21 at 14:50
  • Don't forget about `string_view` if you can use C++17 and above. – mediocrevegetable1 Apr 17 '21 at 14:53
  • since it is a const parameter and it takes some resources to copy a string, even if only updating it's internal reference, from a performance viewpoint it is slightly better the second example with the reference. On the other side by making a copy of the string the compiler has a direct address of the class pointer on the stack within BuildInCommand, with a reference it's indirect. – andreaplanet Apr 17 '21 at 14:54

2 Answers2

0

Since you have that cmd_line characters are not modifiable from the function, I would suggest to use:

BuiltInCommand(const string& cmd_line);

In this way you don't copy the entire string but you pass it by reference (similarly to what you are doing with char*). (In c++ you can use passage by reference, by value or using pointers. For more information check passage by reference)

However, there is also the option of using char* in c++... But I guess you already knew so.

lolloz98
  • 111
  • 5
  • one question, if function A got a reference to string like what u wrote, how can I call function b and send it a new copy of my string? –  Apr 17 '21 at 15:11
  • 1
    *in this way you don't copy the entire string* -- Maybe with C++03, but with the advent of move constructors, your advice is outdated. A good C++ compiler will simply do a `std::move` or similar of the string if it's passed by value. Also, if the function itself takes that reference, and then makes a copy of the string, then it is more advantageous to pass the string by value and not have to do the copy later on inside the function. So in other words, `BuiltInCommand(std::string cmd_line);` is just as good, and maybe more optimal, than `BuiltInCommand(const std::string& cmd_line);` – PaulMcKenzie Apr 17 '21 at 15:37
0

what is the best way to use string here?

It depends on what the function does.

If you intend to store a copy of the string, then BuiltInCommand(std::string) would be a good default choice.

If you don't intend to store a copy, then best choice is typically to not use std::string here. Often a better choice is to use BuiltInCommand(std::string_view).

std::string_view however doesn't require the string to be null terminated. In many cases that isn't a problem, but if the function has to internally use some C API that relies on null termination and that cannot be changed, then it is fine to keep using const char* in C++.

eerorika
  • 232,697
  • 12
  • 197
  • 326