条款23(二):宁以non-member、non-friend替换member函数

条款23:宁以non-member、non-friend替换member函数

Prefer non-member non-friend functions to member.
在上一部分我们知道:

  • non-member non-friend函数比member函数更具备封装性。

而对于这一点,有两件事情值得注意:
(1)这个论述仅适用于non-member non-friend函数。friends函数对class private成员的访问权利和member函数相同。因此,从封装的角度来看,这里的选择关键并不在于member和non-member函数之间,而在于member和non-member non-friend函数之间。
当然,封装并非我们的唯一考量。当我们考虑隐式类型转换时,就应该在member和non-member函数间进行选择了。

(2)只因为在意封装性而让函数“变成class的non-member”并不意味着它“不可以是另一个class的member。”例如,我们可以令clearBrowser称为某个工具类(utility class)的一个static member函数,只要它不是WebBrowser的一部分(或者称为其friend),就不会影响WebBrowser的private成员封装性。

在C++中,比较自然地做法是:

  • 让clearBrowser成为一个non-member函数,并且位于WebBrowser所在的同一个namespace(命名空间)中。
namespace WebBrowserStuff {
    class WebBrowser { ... };
    void clearBrowser(WebBrowser& wb);
    ...
}

然而,这不仅仅是看起来自然而已。

  • namespace和class不同,前者可以跨越多个源码文件而后者不可以。

这一点很重要,因为clearBrowser这个函数是个“提供便利的函数”,如果它既不是member或friend。它就没有对WebBrowser的特殊访问权利!

一个像WebBrowser这样的class可能会拥有大量的便利函数,某些与书签有关,某些与打印有关,某些则与cookie管理有关。。。然而,大多数情况下用户只对其中某一个感兴趣。
因此,分离它们的最直接的办法就是讲某一个相关函数声明在一个头文件内,将另一个相关函数声明在另一个头文件中:

//头文件webbrowser.h,这个头文件针对class WebBrowser自身以及WebBrowser核心机能
namespace WebBrowserStuff {
class WebBrowser { ... };
    ...      //核心机能,例如几乎所用用户都会用到的non-member函数
}

//头文件webbrowserbookmarks.h
namespace WebBrowserStuff {
    ...      //与书签相关的便利函数
}

//头文件webbrowsercookies.h
namespace WebBrowserStuff {
    ...      //与cookie相关的便利函数
}

值得注意的是,这正是C++标准程序库的组织方式。
标准程序库并不是拥有单一、整体、庞大的< C++StandardLibrary >头文件并在其中内含std命名空间内的每一样东西,而是有数十个头文件(< vector >, < algorithm >, < memory> 等等),每个头文件声明std的某些机能。
如果一个用户只想使用vector的相关机能,他并不需要去#include <memory>于是,这就允许用户只对他们所用的那一小部分系统形成编译相依。然而:

  • 这种切割并不使用于class成员函数,因为一个class必须整体定义,不能被分割为片段。

简而言之,将所有便利函数放在多个头文件内但是隶属于同一个命名空间,意味用户可以轻松扩展这一组便利函数。他们需要做的仅仅是添加更多non-member non-friend函数到这个命名空间中。
这一点是class无法提供的另一个性质,因为class定义式对于用户而言是不可扩展的。尽管用户可以派生出新的classes,但是derived classes无法访问base class中被封装(即private)成员,于是此时的“扩展机能”拥有的只是次级身份。另外,在条款7中也讲到:

  • 并非所有的class都被设计作为base classes。

最后:

宁可拿non-member non-friend函数替换member。这样就可以增加封装性、包裹弹性(packing flexibility)和机能扩展性。

a.cpp:9:49: error: non-member function ‘Complex operator-(Complex&)’ cannot have cv-qualifier 9 | friend Complex operator-( Complex &c3 ) const;//重载双目运算符'-' | ^~~~~ a.cpp:25:9: error: no declaration matches ‘Complex Complex::operator-=(Complex&) const’ 25 | Complex Complex::operator-=(Complex &c2)const | ^~~~~~~ a.cpp:8:17: note: candidate is: ‘Complex Complex::operator-=(Complex&)’ 8 | Complex operator-=( Complex &c2 ); //重载双目运算符'-=' | ^~~~~~~~ a.cpp:3:7: note: ‘class Complex’ defined here 3 | class Complex | ^~~~~~~ a.cpp:31:9: error: no declaration matches ‘Complex Complex::operator-(Complex&) const’ 31 | Complex Complex::operator-(Complex &c3)const | ^~~~~~~ a.cpp:31:9: note: no functions named ‘Complex Complex::operator-(Complex&) const’ a.cpp:3:7: note: ‘class Complex’ defined here 3 | class Complex | ^~~~~~~ a.cpp: In function ‘int main(): a.cpp:44:12: error: no match for ‘operator-(operand types are ‘Complex’ and ‘Complex’) 44 | c3 = c1-c2; | ~~^~~ | | | | | Complex | Complex In file included from /usr/include/c++/11/bits/stl_algobase.h:67, from /usr/include/c++/11/bits/char_traits.h:39, from /usr/include/c++/11/ios:40, from /usr/include/c++/11/ostream:38, from /usr/include/c++/11/iostream:39, from a.cpp:1: /usr/include/c++/11/bits/stl_iterator.h:577:5: note: candidate: ‘template<class _IteratorL, class _IteratorR> constexpr decltype ((__y.base() - __x.base())) std::operator-(const std::reverse_iterator<_IteratorL>&, const std::reverse_iterator<_IteratorR>&)’ 577 | operator-(const reverse_iterator<_IteratorL>& __x, | ^~~~~~~~ /usr/include/c++/11/bits/stl_iterator.h:577:5: note: template argument deduction/substitution failed: a.cpp:44:13: note: ‘Complex’ is not derived from ‘const std::reverse_iterator<_IteratorL>’ 44 | c3 = c1-c2; | ^~ In file included from /usr/include/c++/11/bits/stl_algobase.h:67, from /usr/include/c++/11/bits/char_traits.h:39, from /usr/include/c++/11/ios:40, from /usr/include/c++/11/ostream:38, from /usr/include/c++/11/iostream:39, from a.cpp:1: /usr/include/c++/11/bits/stl_iterator.h:1693:5: note: candidate: ‘template<class _IteratorL, class _IteratorR> constexpr decltype ((__x.base() - __y.base())) std::operator-(const std::move_iterator<_IteratorL>&, const std::move_iterator<_IteratorR>&)’ 1693 | operator-(const move_iterator<_IteratorL>& __x, | ^~~~~~~~ /usr/include/c++/11/bits/stl_iterator.h:1693:5: note: template argument deduction/substitution failed: a.cpp:44:13: note: ‘Complex’ is not derived from ‘const std::move_iterator<_IteratorL>’ 44 | c3 = c1-c2; | ^~ a.cpp:9:24: note: candidate: ‘Complex operator-(Complex&)’ 9 | friend Complex operator-( Complex &c3 ) const;//重载双目运算符'-' | ^~~~~~~~ a.cpp:9:24: note: candidate expects 1 argument, 2 provided 确
05-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值