I have a QScopedPointer to a QSqlQuery that is a member variable of a class.
I want to initialize, prepare and execute the QSqlQuery in a method that is a const. The constness of the method will not let me do that.
There are 2 options. Either remove the const from the method or add the keyword mutable to the member variable. Which is better and why?
I read that I should use mutable only when “logical constness” of the method/object is not affected by modifying the variable. What is logical constness and will it be modified in my case?
It really depends on the rest of your class. By preparing a query on your QSqlQuery, you are changing the state of one of the data members. As such, the method is not purely “const”.
What should make your decision is how the rest of your class interacts with the QSqlQuery member. If the state of the query is irrelevant to the state of your object, that is, none of your other methods will care if the prepared query in the QSqlQuery data member has changed, than making the member mutable is a fine option. If changing the prepared query has implications elsewhere, than the method should not be const.