C + + language standards These years have evolved quickly, and many of the newly introduced features have not been used. To be honest, I am also lazy, always feel that I have mastered the C + + has enough to deal with the daily projects, so there is no motivation to learn new features. And always feel that the new introduction of the characteristics of most belong to the kind of "grammatical sugar", can play a small role, in fact, is dispensable.
But recently when I wrote a small program, there was a bugin the codethat bothered me for several days. Finally, with the help of a few enthusiastic netizens to solve. This little bug has made me realize that the override keyword provided in c++11 is very useful, so let's just say the small one in my code. bug.
My code is a use of Qt written GUI Program, the program needs to respond to the mouse mousemoveevent event. However, the result can not receive this event. Here is a simplified version of my program.
Mywidget.h#ifndef mw_h#define mw_h#include <qwidget>class mywidget:public QWidget{ Q_OBJECTpublic: Mywidget (Qwidget *parent = 0); ~mywidget (); Protected:void mousepressevent (Qmouseevent * event), void Mousereleaseevent (Qmouseevent * event), void Mousemoveevent ( Qmoveevent * event);}; #endif
Mywidget.cpp#include "MyWidget.h" #include <qdebug>mywidget::mywidget (Qwidget *parent): Qwidget (parent) {} Mywidget::~mywidget () {}void mywidget::mousepressevent (qmouseevent * Event) {q_unused (event); Qdebug () << "Mousepressevent";} void Mywidget::mousereleaseevent (Qmouseevent * Event) {q_unused (event); Qdebug () << "Mousereleaseevent";} void Mywidget::mousemoveevent (Qmoveevent * Event) {q_unused (event); Qdebug () << "Mousemoveevent";}
Main.cpp#include <QApplication> #include "MyWidget.h" int main (int argc, char **argv) { qapplication app ( ARGC, argv); Mywidget win; Win.show ();
The results of this program run are as follows:
I even thought that the QT version I used was a bug. Later I changed several QT versions, and even tried them under Linux, and the results were the same. Tossing for several days, finally found that I wrote the function prototype wrong. This should be:
void Mousemoveevent (Qmouseevent * event);
I wrote it incorrectly as:
void Mousemoveevent (Qmoveevent * event);
And it happens that QT has qmoveevent this class, so this code compiles without any error hints. The reason why this is wrong is because there is another event in Qt, the prototype is:
void Moveevent (Qmoveevent * event);
At first I mistook this event as a mouse movement event, and later found that there is a mousemoveevent to readily change the function name, but did not pay attention to the following parameter types are different, the result is tragic.
This error is easy to avoid if the override keyword is used.
Mywidget.h#ifndef mw_h#define mw_h#include <qwidget>class mywidget:public QWidget{ Q_OBJECTpublic: Mywidget (Qwidget *parent = 0); ~mywidget (); Protected:void mousepressevent (qmouseevent * Event) override;void mousereleaseevent (qmouseevent * Event) override;void Mousemoveevent (Qmoveevent * event) override;}; #endif
Compile again and you will be prompted
' Void Mywidget::mousemoveevent (Qmoveevent * Event) ' marked override, but does not override
The override keyword, however, requires the compiler to support C++11. If you are using the GCC compiler, you need to add a command line parameter-std=c++11
The override keyword provided in C + +