简体   繁体   中英

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. 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). Whenever the user then requests the updated final matrix, it is recalculated, the result cached, and a const reference returned.

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.

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? 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. Or will it be my saviour?

Any advice will be greatfully received, Cam

Is there a language feature or design paradigm I'm unaware of to help with this?

Perhaps, 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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