myPreciousFunction(std::string s1 = "", std::string s2 = "")
{
}
int main()
{
myPreciousFunction();
}
我可以让参数看起来更漂亮吗? 如果没有提供参数,我希望有空字符串。
最佳答案
你可以这样考虑:
myPreciousFunction(std::string s1 = std::string(), std::string s2 = std::string())
{
}
但它并没有真正看起来更漂亮。
此外,如果您要传递字符串,您可能希望将它们作为 const&
传递:
myPreciousFunction(const std::string& s1, const std::string& s2)
{
}
这是避免处理周围数据的标准方法。
关于c++ - 默认字符串参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2749471/