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

主要原因:non-member、non-friend带来的封装性比member函数高。

 

例如C++的做法:结合了namespace

 

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、付费专栏及课程。

余额充值