当使用返回引用的函数初始化“auto”var 时,为什么 var 类型不是引用?
例如在下面的示例中,为什么 x 的类型是 Foo 而不是 Foo& ?
class TestClass {
public:
Foo& GetFoo() { return mFoo; }
private:
Foo mFoo;
};
int main()
{
TestClass testClass;
auto x = testClass.GetFoo(); // Why type of x is 'Foo' and not 'Foo&' ?
return 0;
}
编辑:该链接解释了如何获取参考,但我的问题是reason对于这种行为。
因为如果那样的话会很烦人。例如,您如何指定您didn't想要参考吗?
当你使用auto
,你需要把const
, &
, &&
, and volatile
在你自己。
auto& x = testClass.GetFoo();
是你的解决方案。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)