我有这个代码.
CGraphicTextInstance*& prGuildNameInstance = pTextTail->pGuildNameTextInstance;
Run Code Online (Sandbox Code Playgroud)
我想应用一些c ++ 11功能,如auto转换.
自动后应该是这样的:
auto prGuildNameInstance = pTextTail->pGuildNameTextInstance;
Run Code Online (Sandbox Code Playgroud)
添加自动后,自动分配只是CGraphicTextInstance没有操作员*&我看到这与智能感知.
我的问题是为什么?我应该添加手动缺少的运营商?像这样 ?
auto &prGuildNameInstance = pTextTail->pGuildNameTextInstance;
Run Code Online (Sandbox Code Playgroud)
我的问题可以破坏我的代码吗?这可能是个问题吗?可以自动分配他想要的东西吗?
auto(大多数情况下)使用与模板推导相同的规则来推导出类型.因此,它将删除引用,因此您需要明确提供引用:
auto prGuildNameInstance = pTextTail->pGuildNameTextInstance;
//prGuildNameInstance is of type CGraphicTextInstance*
auto& prGuildNameInstance = pTextTail->pGuildNameTextInstance;
//prGuildNameInstance is of type CGraphicTextInstance*&
Run Code Online (Sandbox Code Playgroud)
选择与任何变量基本相同:如果您想要参考,请索取参考; 如果你想要一个价值,就要求一个价值.