[英]Define operator! for shared_ptr<A>
我在定義operator !
對於我的 class。 我使用shared_ptr<A>
對象,我想使用!
在他們。
shared_ptr<a> b;
bool result = !b; // using my operator, not !shared_ptr
我嘗試了幾次,但我得到了模棱兩可的錯誤。
shared_ptr b;
bool result = !*b;
bool result = b and !(*b);
shared_ptr
沒有operator!
已定義,但您可以比較它所持有的指針,例如:
shared_ptr<a> b;
bool result = !b.get();
或者,它確實定義了一個operator bool
:
shared_ptr<a> b;
bool result = !(bool)b; // or: !static_cast<bool>(b)
或者,它還為nullptr
定義了operator==
:
shared_ptr<a> b;
bool result = (b == nullptr);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.