C ++

Source: Internet
Author: User

This is the review last week.CodeWhen you notice three points, I feel a little interesting, you may wish to record it.

Do not add virtual functions if necessary.

For example, we have the following hierarchical design of the ball class, in which we need to determine whether a certain ball can be played (kickable ):

 
Class ball {public: Virtual bool iskickable () = 0 ;}; class football {public: Virtual bool iskickable () {returntrue ;}}; class basketball {public: virtual bool iskickable () {returnfalse ;}};

At first glance, I think it is quite reasonable, but think about it carefully. In fact, iskickable is an essential static attribute of a certain ball. It is a waste to use a virtual function to express this information, A more reasonable approach should be to use a data member and a common member function:

 
Class ball {public: bool iskickable () {return m_biskickable;} protected: bool m_biskickable ;}; class football {public: Football (): biskickable (true ){}}; class basketball {public: Basketball (): biskickable (false ){}};

I have met such a design for at least two times, one for review and the other for review. The result is changed to the second method that we think is more reasonable.

Do not use "|" for complicated logic judgment

"|" Is a "or operation" symbol. It is very simple and clear when you actually use it as an OR operation. But someone has invented a tricky method to use it. For exampleProgramThere may be three States: A, B, or C. Now there is a variable Bok. If the current state of the program is C, the bok must be true. How can we assert?
The following is an intuitive method:

 
If (ISC () assert (Bok );

However, some people think that an if statement is troublesome, so they invented:

 
Assert (ISA () | ISB () | Bok );

The logic is as follows: if it is neither a nor B, then Bok must be true.
Although the Code is simplified to only one statement, this poses a challenge to understanding.

We generally do not recommend this inintuitive Method for judgment.

Pure virtual functions and default implementations

There is a base class. We expect it to be an abstract class, but we also expect its virtual functions to be implemented by default. This is actually a syntax problem: we can set a virtual function as pure virtual and provide default implementation. (In the first place, I thought I couldn't do it. I wanted to set the constructor as pretected to achieve a similar effect, but this is not a very reasonable concept.) in this case, I don't think it is necessary to set all functions as pure virtual, and find a typical example. For example, set the Destructor as pure virtual and provide the 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.

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.