小编Rea*_*ria的帖子

C++返回一个字符串引用并在同一个函数中传递一个字符串引用

我和我的上级一直在争论这个功能:

const std::string &GetCurrentDataSourceName(std::string & sName)
{
   sName = GetAnotherComponent().GetName();
   return  sName;
}
Run Code Online (Sandbox Code Playgroud)

是否包含函数返回类型和参数类型的原因?该函数的目的是返回1个值.

他的动机和用例是人们可以做到这一点:

std::string sName = "";
SetSomeValue(GetCurrentDataSourceName(sName));
Run Code Online (Sandbox Code Playgroud)

我认为最好省略这样的参数:

const std::string &GetCurrentDataSourceName()
{
   return  GetAnotherComponent().GetName();
}
Run Code Online (Sandbox Code Playgroud)

但他让我怀疑自己的编码能力.

编辑: 返回的值必须是const.代码也已更新,以显示返回值的来源.我来自同一个类中的另一个组件;

c++ arguments return-type

2
推荐指数
2
解决办法
870
查看次数

标签 统计

arguments ×1

c++ ×1

return-type ×1