c++ - 默认字符串参数

标签 c++

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/

相关文章:

c++ - Qt QAxWidget - WebBrowser 手动设置插槽/信号

c++ - 单例实现 - 为什么需要复制构造函数?

c# - VirtualAlloc 和 StdCall

c++ - 如何检索轮廓点 - 使用 opencv 转换为 vector ?

c++ - 对临时对象的 const 引用在函数作用域(生命周期)后被破坏

c++ - 分配 vs mempcy - 在这种情况下会更快

c++ - 为什么 istream::operator>> 没有采用 char 的重载?

c++ - 视差映射 - GLSL- OpenGL

c++ - 是否可以仅在需要时缩小范围?

c++ - 使用 _Ropen 打开物理文件时出错