C++ 缩短长变量访问器的最佳方法是什么?

C++ 缩短长变量访问器的最佳方法是什么?,c++,class,function,variables,C++,Class,Function,Variables,因此,我有一个场景,其中类中有类,以便访问特定变量或函数: stateMachine->data->poseEstimate->getData() stateMachine->data->poseEstimate->setData() 现在,这是完全合法的,但它看起来错综复杂,很难阅读。在函数中,我希望能够执行以下操作: typedef stateMachine->data->poseEstimate pose pose->getData

因此,我有一个场景,其中类中有类,以便访问特定变量或函数:

stateMachine->data->poseEstimate->getData()
stateMachine->data->poseEstimate->setData()
现在,这是完全合法的,但它看起来错综复杂,很难阅读。在函数中,我希望能够执行以下操作:

typedef stateMachine->data->poseEstimate pose

pose->getData()
pose->setData()

这将使代码更具可读性。显然
typedef
不起作用,因为它是用来定义类型的。是否有一种同样的方法允许我这样做?

使用引用存储中间对象。我们不知道您的类型名称,但假设
poseEstimate
的类型为
MyType

MyType &pose = stateMachine->data->poseEstimate;

pose->getData();
pose->setData();

// ...

实际上,我用一个引用变量为所述对象加上别名,该引用变量具有与其所在上下文相关的描述性名称:

PoseEstimateType& PoseEstimate = stateMachine->data->poseEstimate;
PoseEstimate->getData();
PoseEstimate->setData();
如果编译器支持
auto
关键字,则可以使用
auto
引用:

auto& PoseEstimate = stateMachine->data->poseEstimate;
PoseEstimate->getData();
PoseEstimate->setData();

有什么原因不能创建一个引用上述变量的引用变量,但该变量的名称较短吗?一点也不,我认为会有更好的替换方法。这种情况是否可能是由于缺少干净的接口造成的?例如,这可以用一个帮助器简化,比如<代码> StimeMaCHIN::GETPOSEEAUTIVE()/CUT>?或者更好的使用<代码> Auto <代码>,如果你的C++版本支持它建议引用。指针授予的访问权限超出了需要。参考文献给出的正是我们想要的,不多也不少。除非有重新放置引用的意图,这当然是不可能的。在C语言中有可能做类似的事情吗?直接使用指针会产生错误:
错误:获取临时[-fppermissive]的地址。