0
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

これはすべて新しいことですが、これを処理する方法はありますか?

4

2 に答える 2

2

const 参照渡し:

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

どちらのフォームもそれにバインドでき、印刷するものを変更する必要はありません。

于 2013-01-31T10:09:41.657 に答える
1

参照によって渡された文字列を変更する予定がない限り、単一の

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

動作します。

それとも、各バージョンで何か違うことをしたいと思っていますか?

于 2013-01-31T10:10:06.063 に答える