Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
QSharedData and mutable members: Calling detach() from const method?
Asperamanca last edited by
My (implicitly shared) class has an error flag. I set this flag to indicate that the data within should not be further used.
To keep my class at pointer size, I decided to put my error flag in my private data class, derived from QSharedData.
Unfortunately, there are one or two cases where I recognize an error in a const method. The logical solution was to make my error flag mutable within the private data class. However, this is bound to break the detach mechanism.
I could call detach myself in this case. However, detach is non-const.
Would it be safe in such circumstances to cast away the constness of "this" (the class holding the private data class) in order to detach, before setting the error flag?
(I should note that this would be my first const_cast in several 10000 lines of code. I'm no fan of them).