Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 (i.e. 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

like image 830
cmannett85 Avatar asked Dec 20 '10 22:12

cmannett85


1 Answers

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.

like image 134
CB Bailey Avatar answered Sep 22 '22 05:09

CB Bailey