簡體   English   中英

定義運算符! 對於 shared_ptr<a></a>

[英]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; 

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM