[英]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.