2012-10-18 72 views
1

所以我在那里存在着阶级内部类,以便在访问特定变量或函数的一个场景:缩短长变量访问器的最佳方式是什么?

stateMachine->data->poseEstimate->getData() 
stateMachine->data->poseEstimate->setData() 

现在,这是完全合法的,但它看起来令人费解和难以阅读。在功能我想能够做这样的事情:

typedef stateMachine->data->poseEstimate pose 

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

这将使代码更具可读性。显然typedef不会工作,因为它用于定义类型。有没有同样的方法可以让我做到这一点?

+0

创建一个变量。 – chris

+3

是否有任何理由,你不能做一个引用变量,但有一个较短的名称参考变量? –

+0

一点都不,我认为会有更好的替代方法。 –

回答

2

在实践中,我别名说与参考变量对象给出有关它在上下文中的描述性名称:

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

如果你的编译器支持auto关键字,您可以使用auto参考:

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

使用引用存储中间对象。我们不知道你的类型名,但假设poseEstimateMyType类型:

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

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

// ... 
+2

或更好使用'auto' –

+0

建议使用参考。指针授予比必要的更多访问权限。参考文献给出了所需的内容,不多不少。除非有意重新提供参考,这当然是不可能的。 –

+0

@SionSheevok好点,谢谢 – pb2q

相关问题