QOpenGLFunctions not const correct

我怕爱的太早我们不能终老 提交于 2020-03-23 01:16:11

问题


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:

  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.



来源:https://stackoverflow.com/questions/23841355/qopenglfunctions-not-const-correct

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!