转换int&串起来

时间:2016-12-21 17:12:46

标签: c++ string int pass-by-reference

这对普通的C ++用户来说似乎微不足道,但我现在正在重新学习C ++。我有一个函数,它接受int&,我想将该值添加到print语句的字符串。所以,我有这样的事情:

std::string getTest(void* dataGroup, int& xWidth, int& yHeight)
{
     std::string results = "";

     results += "\nxWidth is: " + xWidth;

     return results;
}

我打电话时无法运行。转换pass-by-reference int&的正确方法是什么?值是否可以添加到字符串中?

1 个答案:

答案 0 :(得分:3)

std::to_string(xWidth)

&几乎无关紧要。 to_string按值int获取,因此您的函数如何使用它并不重要:按值,左值引用,右值引用,const或非const ......一切都会奏效。

相关问题