Detailed explanation of C + + member functions override and final descriptor usage _c language

Source: Internet
Author: User
Tags reserved

Override specifier

You can use the Override keyword to specify a member function that overrides a virtual function in a base class.
Grammar

Function-declaration Override;

Note
Override is context-sensitive and has special meaning only when it is used after a member function declaration, otherwise it is not a reserved keyword.
Using override helps to prevent unexpected inheritance behavior from occurring in your code. The following example demonstrates that you might not intend to use the member function behavior of a derived class without using override. The compiler does not emit any errors for this code.

Class BaseClass
{
  virtual void Funca ();
  virtual void FUNCB () const;
  virtual void funcc (int = 0);
  void Funcd ();

Class Derivedclass:public BaseClass
{
  virtual void Funca ();/OK, works as intended

  virtual void funcb (); /DERIVEDCLASS::FUNCB () is non-const, so it does not
             //override BASECLASS::FUNCB () const and it are new member Func tion

  virtual void funcc (double = 0.0);//DERIVEDCLASS::FUNCC (double) has a different
                   //parameter type than Bas ECLASS::FUNCC (int), so
                   /DERIVEDCLASS::FUNCC (double) are a new member function

};

When using override, the compiler generates an error without creating a new member function without prompting.

Class BaseClass
{
  virtual void Funca ();
  virtual void FUNCB () const;
  virtual void funcc (int = 0);
  void Funcd ();

Class Derivedclass:public BaseClass
{
  virtual void Funca () override;//OK

  virtual void funcb () override;//  Compiler ERROR:DERIVEDCLASS::FUNCB () does not 
                  //override BASECLASS::FUNCB () const

  virtual void FUNCC (double = 0.0) override; Compiler error: 
                         //DERIVEDCLASS::FUNCC (double) does not 
                         //override BASECLASS::FUNCC (int)

  void FUNCD () override; Compiler ERROR:DERIVEDCLASS::FUNCD () does not 
              //override the Non-virtual Baseclass::funcd ()
};


To specify that a function cannot be overridden and cannot inherit a class, use the final keyword.


Final Descriptor
You can use the final keyword to specify a virtual function that cannot be overridden in a derived class. You can also use it to specify a class that cannot be inherited.
Grammar

Function-declaration final;

Class Class-name Final base-classes

Note
Final is context-sensitive and has special meaning only if it is used after a function declaration or a class name, otherwise it is not a reserved keyword.
When final is used in a class declaration, base-classes is an optional part of the declaration.
The following example uses the final keyword to specify that a virtual function cannot be overridden.

Class BaseClass
{
  virtual void func () final;

Class Derivedclass:public BaseClass
{
  virtual void func ();//compiler error:attempting to 
             //override a fi nal function
};

For information about how to specify that you can override member functions, see the override specifier.
The next example uses the final keyword to specify that a class cannot be inherited.

Class BaseClass final 
{
};

Class Derivedclass:public BaseClass//Compiler Error:baseclass is 
                   //marked as non-inheritable
{
};

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.