In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces what is the use of override in Clover 11, it has a certain reference value, interested friends can refer to it, I hope you can learn a lot after reading this article, let the editor take you to understand it.
1. Public inheritance
Public inheritance includes two parts: one is "function interface" (interface), and the other is "function implementation" (implementation).
For example, in the Shape class, there are three member functions that correspond to three inheritance methods:
Class Shape {public: virtual void Draw () const = 0; / / 1) Pure virtual function virtual void Error (const string& msg); / / 2) ordinary virtual function int ObjectID () const; / / 3) non-virtual function}; class Rectangle: public Shape {...}; class Ellipse: public Shape {...}; 1.1 pure virtual function (pure virtual) Shape * ps1 = new Rectangle;ps1- > Draw () / / calls Rectangle::DrawShape * ps2 = new Ellipse;ps2- > Draw (); / / calls Ellipse::Draw
A pure virtual function inherits the interface of the member function in the base class, and overrides the implementation of the member function in the derived class
To call the Draw () of the base class, add the class scope operator:
Ps1- > Shape::Draw (); / / calls Shape::draw 1.2 ordinary virtual functions
A normal virtual function defines a default implementation (default implementation) in the base class, indicating that it inherits the base class member function interface and the default implementation, and the derived class chooses whether to override the function.
In fact, it is dangerous to allow ordinary virtual functions to inherit both the interface and the default implementation. As follows, ModelA and ModelB are two types of Airplane aircraft, and they fly in exactly the same way.
Class Airplane {public: virtual void Fly (const string& destination);}; class ModelA: public Airplane {...}; class ModelB: public Airplane {...};
This is a typical object-oriented design. If the two classes share a feature-- Fly, then Fly can be implemented in the base class and inherited by two derived classes.
Now add another aircraft model, ModelC, which flies differently from ModelA,ModelB. If you accidentally forget to rewrite the new Fly function in ModelC
Class ModelC: public Airplane {. / / no fly function is declared};
Then call the fly function in ModelC, that is, call Airplane::Fly, but the flight mode of ModelC is not the same as the default
Airplane * pa = new ModelC;pa- > Fly (Qingdao); / / calls Airplane::fly!
As mentioned earlier, it is dangerous for ordinary virtual functions to inherit both the interface and the default implementation, preferably implementing the default behavior (behavior) in the base class, but providing this default behavior only if the derived class requires it
1.2.1 method one
One method is pure virtual function + default implementation, because it is pure virtual function, so only the interface is inherited, its default implementation will not be inherited. For a derived class to use the default implementation, it must explicitly call the
Class Airplane {public: virtual void Fly (const string& destination) = 0;}; void Airplane::Fly (const string& destination) {/ / a pure virtual function default code for flying an airplane to the given destination} class ModelA: public Airplane {virtual void Fly (const string& destination) {Airplane::Fly (destination);}};
In this way, in the derived class ModelC, even if you accidentally forget to override the Fly function, the default implementation of Airplane will not be called.
Class ModelC: public Airplane {public: virtual void Fly (const string& destination);}; void ModelC::Fly (const string& destination) {/ / code for flying a ModelC airplane to the given destination} 1.2.2 method II
As you can see, the crux of the above problem is that you accidentally forget to rewrite the fly function in the derived class ModelC, and the keyword override is used in Clippers 11 to avoid such "carelessness".
1.3 non-virtual function
The non-virtual member function has no virtual keyword, which means that the derived class inherits not only the interface, but also a mandatory implementation (mandatory implementation).
Now that you inherit a mandatory implementation, there is no need to redefine the member functions inherited from the base class in the derived class, as follows:
If the ObjectID function is called with a pointer, it is all called Shape::ObjectID ().
Rectangel rc; / / rc is an object of type RectangleShape * pB = & rc; / / get pointer to rcpB- > ObjectID (); / / call ObjectID () through pointerRectangle * pD = & rc; / / get pointer to rcpD- > ObjectID (); / / call ObjectID () through pointer
What if the member function ObjectID inherited from the base class is redefined in the derived class?
Class Rectangel: public Shape {public: int ObjectID () const; / / hides Shape::ObjectID}; pB- > ObjectID (); / / calls Shape::ObjectID () pD- > ObjectID (); / / calls Rectagle::ObjectID ()
At this point, the redefined member function in the derived class will "hide" the member function inherited from the base class.
This is because non-virtual functions are "statically bound", and pB is declared as a pointer of type Shape*, then all non-virtual functions called through pB are in the base class, even if pB points to a derived class.
The opposite of "static binding" is the "dynamic binding" of virtual functions, that is, regardless of whether pB is declared as Shape* or Rectangle*, the virtual function it calls depends on the type of object that pB actually points to.
2 rewrite (override)
The override keyword is mentioned in 1.2.2 to avoid the error of forgetting to override virtual functions in derived classes.
Here are four mistakes that are easy to make when rewriting a virtual function as an example.
Class Base {public: virtual void mf1 () const; virtual void mf2 (int x); virtual void mf3 () &; void mf4 () const; / / is not declared virtual in Base}; class Derived: public Base {public: virtual void mf1 (); / / declared const in Base, but not in Derived. Virtual void mf2 (unsigned int x); / / takes an int in Base, but an unsigned int in Derived virtual void mf3 () & &; / / is lvalue-qualified in Base, but rvalue-qualified in Derived. Void mf4 () const;};
In a derived class, when overriding (override) inherits from the implementation (implementation) of the base class member function, the following conditions are met:
A virtual: in the base class, the member function is declared as virtual (virtual)
Erong: in both base and derived classes, the return type and exception specification (exception specification) of member functions must be compatible.
Four sameness: in base and derived classes, the member function name, formal parameter type, constant attribute (constness) and reference qualifier (reference qualifier) must be exactly the same
There are so many restrictions that lead to virtual function rewriting such as the above code, which is very easy to make an error due to an accident.
The override keyword in Clippers 11 can explicitly declare in a derived class which member functions need to be overridden, and if not, the compiler will report an error.
Class Derived: public Base {public: virtual void mf1 () override; virtual void mf2 (unsigned int x) override; virtual void mf3 () & & override; virtual void mf4 () const override;};
Therefore, even if some harsh condition of virtual function rewriting is accidentally omitted, the error can be quickly corrected by the compiler.
Class Derived: public Base {public: virtual void mf1 () const override; / / adding "virtual" is OK, but not necessary virtual void mf2 (int x) override; void mf3 () & override; void mf4 () const override;}; summary:
1) Public inheritance
Pure virtual function = > inherits: interface (interface)
Ordinary virtual function = > inherits: interface + default implementation (default implementation)
Non-virtual member function = > inherits: interface + mandatory implementation (mandatory implementation)
2) do not redefine a non-virtual function inherited from the base class (never redefine an inherited non-virtual function)
3) after declaring the function that needs to be overridden, add the keyword override
Thank you for reading this article carefully. I hope the article "what is the use of override in Category 11" shared by the editor will be helpful to you. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.