Current location - Quotes Website - Signature design - Why do I need to override the keyword solution?
Why do I need to override the keyword solution?
C++ does not have this keyword. When you declare a virtual function, you only need to add the keyword virtual before the function declaration, but when the virtual function is overloaded, virtual is optional. Even if virtual is written in the subclass, it is not clear whether this function is declared for the first time or overloads the virtual function in the base class. More seriously, there is no way to know whether a function is a virtual function without looking at the declaration of the base class and using comments (I mean, programmers don't know when they look at the source code, and runtime methods are beyond the scope of this discussion). Once I sorted out the C++ code I wrote a few months ago and saw that one of the class member functions only returned the value of a member variable. I think such a member function should be declared as const, which can also be applied to the case of const instance. But in this way, the running result of the program is incorrect. After careful tracing, it is found that this function was originally declared as a virtual function in the base class, and this version in the subclass is actually an overloaded version, but after I added const, its signature is different from that in the base class, so the compiler no longer thinks that it overloads the virtual function in the base class, but is a brand-new function. The compiler can't give any useful information about this, so it can only be checked bit by bit manually. However, if you need C# keywords like C#, then the overloaded version in the subclass must be override, so you can know it is an overloaded method at a glance, and we will not change its signature at will.

Override requirements are:

1 For non-overloaded methods, the override keyword cannot be added, otherwise a compilation error will occur.

2 For overloaded methods, the override keyword must be used; If you want to hide the methods in the base class, you need to use the new keyword.

3 With IDE, as long as you enter override and space in Visual Studio.Net, you can automatically list all overloaded methods in the base class.

In this respect, I think that with the help of typedef, template or macro function of C++, we may be able to simulate the function of override keyword.

One of my shapeless ideas is:

1 Define VIRTUAL as virtual, and then define the name of the written virtual function as illegal. Overloading directly in subclasses will lead to compile-time errors.

2 To define OVERRIDE, first check whether the overloaded function name is illegally defined. If not, define an illegal result, resulting in a compile-time error, so that the OVERRIDE keyword cannot be applied to non-overloaded functions.

If there is an illegal definition, let it first cancel the illegal definition of the overloaded function name, then overload it normally and redefine it illegally again, so that the virtual function can be overloaded correctly.

However, there are still some problems, that is, there will be some problems when writing the implementation of overloaded functions and calling such declared functions, because the corresponding function names have been defined as illegal and cannot be used directly. More complex macro definitions may be needed to solve it.

The idea is this, but there are many problems in concrete implementation. I haven't succeeded in the experiment yet, so I keep my ideas here for future reference.