c ++使用const char*参数类型调用函数的最佳方法

ahm*_*n86 0 c++ pointers function char parameter-passing

使用以下声明调用函数的最佳方法是什么?

string Extract(const char* pattern,const char* input);
Run Code Online (Sandbox Code Playgroud)

我用

string str=Extract("something","input text");
Run Code Online (Sandbox Code Playgroud)

这个用法有问题吗?

我应该使用以下内容吗?

char pattern[]="something";
char input[]="input";
//or use pointers with new operator and copy then free?
Run Code Online (Sandbox Code Playgroud)

两者都有效,但我喜欢第一个,但我想知道最佳实践.

reu*_*ben 6

文字字符串(例如"something")可以很好地作为const char*函数调用的参数.