所以我有一个场景,其中类中有类,以便访问特定变量或函数:
stateMachine->data->poseEstimate->getData()
stateMachine->data->poseEstimate->setData()
现在这是完全合法的,但它看起来很复杂并且难以阅读。在函数中我希望能够做这样的事情:
typedef stateMachine->data->poseEstimate pose
pose->getData()
pose->setData()
这将使代码更具可读性。显然 typedef
不会工作,因为它用于定义类型。有没有一种平等的方式可以让我做到这一点?
最佳答案
在实践中,我用一个引用变量给所述对象起别名,给出一个与其所在上下文相关的描述性名称:
PoseEstimateType& PoseEstimate = stateMachine->data->poseEstimate;
PoseEstimate->getData();
PoseEstimate->setData();
如果你的编译器支持auto
关键字,你可以使用auto
引用:
auto& PoseEstimate = stateMachine->data->poseEstimate;
PoseEstimate->getData();
PoseEstimate->setData();
关于c++ - 缩短长变量访问器的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12945446/