Skip to content

Commit

Permalink
==, != operator overloading
Browse files Browse the repository at this point in the history
练习12.20的 sbp != sb.end() 报错,原因是无法对两个StrBlobPtr进行默认的 != 操作,需要对该运算符进行重载。
  • Loading branch information
ZainChenDev authored Apr 17, 2023
1 parent 7126096 commit edabb4d
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions excersize/ch12.md
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,8 @@ private:
class StrBlobPtr
{
friend bool operator==(const StrBlobPtr&,const StrBlobPtr&);
friend bool operator!=(const StrBlobPtr&,const StrBlobPtr&);
public:
StrBlobPtr() :curr(0) {}
StrBlobPtr(StrBlob &a, size_t sz = 0) :wptr(a.data), curr(sz) {}
Expand Down Expand Up @@ -580,6 +582,17 @@ StrBlobPtr StrBlob::end()
{
return StrBlobPtr(*this, data->size());
}
bool operator==(const StrBlobPtr& lhs, const StrBlobPtr& rhs) {
if (!lhs.wptr.expired() && !rhs.wptr.expired()) {
return (lhs.wptr.lock() == rhs.wptr.lock()) && (lhs.curr == rhs.curr);
} else {
return false;
}
}
bool operator!=(const StrBlobPtr& lhs, const StrBlobPtr& rhs) {
return !(lhs==rhs);
}
```

## 练习12.20
Expand Down

0 comments on commit edabb4d

Please sign in to comment.