问题
Assume a QOpenGLFunctions
object is a member of a class. Since the gl*
methods are not marked const where appropriate, they cannot be invoked in a const method even if they may make no state changes at all.
Am I using QOpenGLFunctions
wrong?
回答1:
It's somewhat philosophical, but I could argue that you're using it "wrong". By making QOpenGLFunctions
a member of your class, you're saying that the OpenGL functions are part of your class state. Which they are really not. They are something your class uses, but they are not part of your class.
You have a couple of options to fix this:
- You can add a
mutable
to the member definition (mutable QOpenGLFunctions m_...
). - You can make the member variable a pointer to
OpenGLFunctions
, and allocated the object in the constructor of your class.
Using mutable
always feels kind of hacky to me, even though it has legitimate uses. I clearly prefer option 2 in this case. Beyond solving your const-correctness problem, it also expresses the weaker coupling between your class and OpenGLFunctions
that really matches the correct relationship I explained in the introduction.
来源:https://stackoverflow.com/questions/23841355/qopenglfunctions-not-const-correct