简体   繁体   English

懒惰的评估和const正确性的问题

[英]Lazy evaluation and problems with const correctness

I have made an openGL camera class that uses lazy evaluation to provide the final projection or model-view-projection matrices through getter functions. 我制作了一个openGL相机类,它使用延迟评估通过getter函数提供最终投影或模型 - 视图 - 投影矩阵。 The user provides the various camera parameters throughout the life of the instance (FOV, position, etc. ), but rather than have the projection matrix and/or MVP matrix recalculated every time a parameter is changed, a 'changed' flag is set (ie the old cached matrix is now invalid). 用户在实例的整个生命周期(FOV,位置等)中提供各种相机参数,但不是每次更改参数时都重新计算投影矩阵和/或MVP矩阵,而是设置“已更改”标志(即旧的缓存矩阵现在无效)。 Whenever the user then requests the updated final matrix, it is recalculated, the result cached, and a const reference returned. 只要用户随后请求更新的最终矩阵,就会重新计算,结果缓存,并返回const引用。

Everything sounds fine until I call my: 一切都听起来不错,直到我打电话给我:

const QMatrix4x4& oE_GLCamera::getModelViewProjection() const;

function from a const oE_GLCamera instance... I use const references everywhere in my app to extract camera data from CAD viewports without changing the camera, but my getter function performs lazy evaluation on member variables if they are invalid - therefore breaking const-correctness. 来自const oE_GLCamera实例的函数...我在我的应用程序中使用const引用来从CAD视口中提取相机数据而不更改相机,但是如果它们无效,我的getter函数会对成员变量执行惰性求值 - 因此会破坏const正确性。

Is there a language feature or design paradigm I'm unaware of to help with this? 是否有语言功能或设计范例我不知道如何帮助解决这个问题? Or is lazy evaluation fundamentally incompatible with const-correctness? 或者懒惰的评估从根本上与const正确性不兼容? I am aware of const_cast<>, I have never used it myself but have a read few things about it which boil down to: If you have you use it, you have already gone wrong somewhere. 我知道const_cast <>,我自己从来没有使用它,但读了一些关于它的事情归结为:如果你使用它,你已经在某个地方出错了。 Or will it be my saviour? 还是会成为我的救世主?

Any advice will be greatfully received, Cam Cam,任何建议都会得到很好的接受

Is there a language feature or design paradigm I'm unaware of to help with this? 是否有语言功能或设计范例我不知道如何帮助解决这个问题?

Perhaps, mutable ? 或许, mutable吗?

A member of a class that is marked as mutable is always non- const even if it is accessed via a reference or pointer to the owning class which is a const reference or a pointer to const . 标记为mutable的类的成员始终是非const即使它是通过引用或指向拥有类的指针访问的,该类是const引用或指向const的指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM