Warning: overrides a destructor but is not marked 'override'
-
Dear developers,
I have derived SlaveThread class from QThread class and defined destructor of SlaveThread Class as follows:
SlaveThread::~SlaveThread() { m_mutex.lock(); m_quit = true; m_mutex.unlock(); wait(); }
After compiling Qt code I get following error.
'~SlaveThread()' overrides a destructor but is not marked 'override'
Can you please inform me what can be reason and solution for above problem?
Thank you very much :)
-
@saurabh162 said in Warning: overrides a destructor but is not marked 'override':
Can you please inform me what can be reason and solution for above problem?
reason:
https://www.geeksforgeeks.org/virtual-function-cpp/solution:
public: .... ~SlaveThread() override;
-
@saurabh162
https://stackoverflow.com/questions/17923370/override-identifier-after-destructor-in-c11And solution as @J-Hilk has just posted.
-
-Wno-inconsistent-missing-destructor-override
-
@Konstantin-Tokarev
Good spot, and https://stackoverflow.com/questions/51612041/disable-a-specific-warning-in-qtcreator/51621828 even shows OP where to put it in Qt Creator :) -
Hallo @J-Hilk ,@JonB , @Konstantin-Tokarev,
Thank you very much for your help.
Now I understand main reason of this problem. Additionally in this post I see two possible solution to remove this warning.
- Using override keyword
- Suppress warning related to override function in Clang code model using ```
"-Wno-inconsistent-missing-destructor-override"
Can you please inform me which one above is more in direction of clean coding guidelines? According to me, using keyword "override" is right approach to solve this problem. Whether I am right ? Many thanks :)
-
@saurabh162
The warning is there because it's a warning! So (I would have thought) for choice you should put in theoverride
rather than suppress the warning. However, that requires code edits. Sounds like that is OK for you, but for others it might not be, so the warning suppression is for them. -
-
@saurabh162 it's a warning for you the developer.
The override keyword can help avoiding bugs, by producing a compilation error when the intended override isn't technically an override.
E.g. that the function type isn't exactly like the base class function. Or that a maintenance of the base class changes that function's type (adding a defaulted argument for example)As long as the base class has the destructor marked as virtual, all derived class destructors will call the basal destructor correctly.
In your case it doesn't matter much. But just in case you're actually using virtual functions yourself in your other classes, you should stick with the override keyword.
-
For destructor
override
keyword can only matter if it's the only one virtual function in the class that is overridden, and this case is covered by-Wdelete-non-virtual-dtor
-
Hello @JonB, @J-Hilk, @Konstantin-Tokarev,
Once again thank you very much for your help.
Now I have understood it correctly.