Skip to content

Commit edabb4d

Browse files
authored
==, != operator overloading
练习12.20的 sbp != sb.end() 报错,原因是无法对两个StrBlobPtr进行默认的 != 操作,需要对该运算符进行重载。
1 parent 7126096 commit edabb4d

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

excersize/ch12.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,8 @@ private:
544544
545545
class StrBlobPtr
546546
{
547+
friend bool operator==(const StrBlobPtr&,const StrBlobPtr&);
548+
friend bool operator!=(const StrBlobPtr&,const StrBlobPtr&);
547549
public:
548550
StrBlobPtr() :curr(0) {}
549551
StrBlobPtr(StrBlob &a, size_t sz = 0) :wptr(a.data), curr(sz) {}
@@ -580,6 +582,17 @@ StrBlobPtr StrBlob::end()
580582
{
581583
return StrBlobPtr(*this, data->size());
582584
}
585+
586+
bool operator==(const StrBlobPtr& lhs, const StrBlobPtr& rhs) {
587+
if (!lhs.wptr.expired() && !rhs.wptr.expired()) {
588+
return (lhs.wptr.lock() == rhs.wptr.lock()) && (lhs.curr == rhs.curr);
589+
} else {
590+
return false;
591+
}
592+
}
593+
bool operator!=(const StrBlobPtr& lhs, const StrBlobPtr& rhs) {
594+
return !(lhs==rhs);
595+
}
583596
```
584597

585598
## 练习12.20

0 commit comments

Comments
 (0)