user2848971
user2848971

Reputation:

QOpenGLFunctions not const correct

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?

Upvotes: 2

Views: 575

Answers (1)

Reto Koradi
Reto Koradi

Reputation: 54592

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:

  1. You can add a mutable to the member definition (mutable QOpenGLFunctions m_...).
  2. 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.

Upvotes: 1

Related Questions