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
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With