Share via


shared_ptr::use_count

Counts numbers of resource owners.

long use_count() const;

Remarks

The member function returns the number of shared_ptr objects that own the resource that is owned by *this.

Example

 

// std_tr1__memory__shared_ptr_use_count.cpp 
// compile with: /EHsc 
#include <memory> 
#include <iostream> 
 
int main() 
    { 
    std::shared_ptr<int> sp1(new int(5)); 
    std::cout << "sp1.use_count() == " 
        << sp1.use_count() << std::endl; 
 
    std::shared_ptr<int> sp2(sp1); 
    std::cout << "sp1.use_count() == " 
        << sp1.use_count() << std::endl; 
 
    return (0); 
    } 
 
sp1.use_count() == 1
sp1.use_count() == 2

Requirements

Header: <memory>

Namespace: std

See Also

Reference

shared_ptr Class