繁体   English   中英

歧义调用重载函数的解决方法

[英]Workaround for ambiguous call to overloaded function

void printOutput(std::string text);
void printOutput(std::string& text);

这两个函数都会向控制台输出一些文本,但是我想处理以下每种情况:

std::string testOutput = "asdf";
output->printOutput(testOutput); // Gives the error as it can use either function

在某些情况下,我可能想要:

output->printOutput("asdf"); // Only the first function can be used

这一切都不是新事物,有没有办法我可以处理?

通过const参考传递:

void printOutput(const std::string &text);

两种形式都可以绑定到该表格,而您不必修改打印内容。

除非您打算修改通过引用传递的字符串,否则单个

void printOutput(std::string const& text);

将工作。

还是您希望每个版本都做一些不同的事情?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM