In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Next, please follow the editor to study!
The function of the virtual function is to realize dynamic binding, that is, to dynamically select the appropriate member function during the running phase of the program. after defining the virtual function, the virtual function can be redefined in the derived class of the base class. the function redefined in the derived class should have the same number and type of formal parameters as the virtual function.
If it is not necessary, do not increase the virtual function
For example, we have the following class hierarchy design for balls, in which we need to determine whether a ball is kickable (kickable):
Class Ball {public: virtual bool IsKickable () = 0;}; class Football {public: virtual bool IsKickable () {return true;}}; class Basketball {public: virtual bool IsKickable () {return false;}}
At first glance, it seems reasonable, but when you think about it, IsKickable is actually the essential static property of some kind of ball. It is a waste to use a virtual function to represent this information. A more reasonable way is to use a data member and a common member function:
I have come across a design like this at least twice, once by review and once by review, and the result is changed to the second way that we think is more reasonable.
Do not use "| |" to make complex logical judgments.
The symbol "| |" or operation "is really simple and straightforward when you do take it as or operate it." But someone invented a way to compare tricky to use it.
For example, our program may have three states: a, B, or C. now there is a variable bOk. If the current state of the program is C, bOk must be true. How to assert? Generally speaking, the more intuitive approach is:
If (IsC ()) assert (bOk)
But some people find it troublesome to have an if judgment, so they invent:
Assert (IsA () | | IsB () | | bOk)
Pure virtual function and default implementation
There is a base class that we expect to be an abstract class, but at the same time we expect its virtual functions to have a default implementation. This is actually a grammatical problem: we can make a virtual function pure virtual while providing a default implementation. (but at first I thought it would not work. I wanted to set the constructor to pretected to achieve a similar effect, but this is not very reasonable conceptually.)
In this case, I don't think it's necessary to make all the functions pure virtual and find a typical one, such as setting the destructor to pure virtual and providing a default implementation:
Class Base {public: virtual ~ Base () = 0;}; Base::~Base () {printf ("~ Base ()\ n");} class Derive: public Base {public: virtual ~ Derive () {printf ("~ Derive ()\ n");}}
In this way, the base class is already an abstract class and should be an acceptable solution.
At this point, the study of "what are the three things of C++" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.