How does shared_ptr work in if condition

后端 未结 2 1369
既然无缘
既然无缘 2020-12-17 18:04

In C++, I can write something like:

shared_ptr a_sp = someFunctionReturningSharedPtr();
if (a_sp) {
    cout << a_sp->someData << end         


        
2条回答
  •  情歌与酒
    2020-12-17 18:35

    shared_ptr has operator bool(), which returns true if it is not empty.

    For example, this is Microsoft implementation of shared_ptr::operator bool(): http://msdn.microsoft.com/en-us/library/bb982901.aspx

    shared_ptr::operator boolean-type - Tests if an owned resource exists.

提交回复
热议问题