qt override
时间: 2023-12-10 20:02:42 浏览: 35
In Qt, override is a C++11 keyword that allows a derived class to indicate that it is intending to override a virtual function of its base class. When a function is marked with the override keyword, the compiler will check that there is a virtual function in the base class with the same name, and that the function in the derived class has the correct signature (i.e. same return type, parameter types, and constness).
The use of override is considered good practice because it helps to prevent errors and improve code clarity. It makes it clear which functions in a derived class are intended to override base class functions, and helps to catch errors if the signature of the function in the derived class does not match the signature of the function in the base class.
Here is an example of using override in Qt:
```cpp
class MyBaseClass {
public:
virtual void myVirtualFunction(int val) const;
};
class MyDerivedClass : public MyBaseClass {
public:
void myVirtualFunction(int val) const override;
};
void MyDerivedClass::myVirtualFunction(int val) const {
// implementation of the function
}
```
In this example, the MyDerivedClass class is declaring that it intends to override the myVirtualFunction() function from its MyBaseClass base class. The override keyword is used to indicate this intention, and the function in the derived class has the same name, return type, and parameter types as the function in the base class.