C ++ 11: use the override keyword

override keyword

Action: in the member function declaration or definition, override function is a virtual function to ensure that the override and from base class virtual function.
Position: operator after the function call, the function or pure virtual function identifier "= 0" before.

After using the following benefits:
1. When a comment can use, easy to read.
2. Tell people you read the code, which is replication methods.
3. The compiler can give you a verification override the corresponding method name if you are the parent of all, if not the error.

Application Example override

If you want to override the parent class method, such as toString () method:

the correct one is:

public String toString() override {
    ...
}

If not careful method name wrong but I did not write override, then the compiler can compile because the compiler that this method is your own subclass added methods. Such as:

// 注意这里的小写方法,实际上是错误的。
public String tostring() {...}

Conversely, if you're witty, knowing the parent class of their own to be rewritten, after adding the override tag, the compiler will rewrite check out the wrong way, you will ensure that override the parent class method correctness.
  
  
  
Summary: when rewriting method, preferably with this override keyword to strengthen the code specifications.

Guess you like

Origin www.cnblogs.com/schips/p/12309999.html