Clause 23: Ning to Non-member, Non-friend, replace the member function.

Source: Internet
Author: User

Consider the following frequently occurring use patterns:

class webbroswer{public    : ...     void ClearCache ();     void clearhistory ();     void removecookies ();    ...};

Then it would be natural to think of adding such a clean-up method:

class webbrowser{public    : ...     void cleareverything ();};

Or a way of cleaning up:

void clearbrowser (WebBrowser & WB) {    wb.clearcache ();    Wb.clearhistory ();    Wb.clearcookie ();}
If you want to choose between a member function with the same functionality and a non-member, Non-friend function, then a high encapsulation is non-member, non-friend function. (Pay attention to the encapsulation here, let a function become a class of non-member), does not affect its become another class member function, such as the above Clearbrowser function, it can be completely as a tool class static member function) It is generally good practice to have clearbrowser placed in the same namespace as Clss WebBrowser. Like this:
namespace webbrowserstuff{    class  webbrowser{...    }; void clearbrowser (WebBrowser & WB); // convenience functions such as clearbrowser can declare multiple in a namespace. };
And because namespace can exist across header files, it is also convenient for such tool functions to maintain cross-header files. Summary: Remember that you should replace the member function with the Non-member non-friend function, which can increase encapsulation, wrap elasticity, and function expansion properties.

Clause 23: Ning to Non-member, Non-friend, replace the member function.

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.