weak_ptr::lock
Obtient la propriété exclusive d'une ressource.
shared_ptr<Ty> lock() const;
Notes
La fonction membre retourne un objet vide de shared_ptr si *this a expiré ; sinon retourne un objet de shared_ptr, classe<Ty> qui possède la ressource *this affiche.
Exemple
// std_tr1__memory__weak_ptr_lock.cpp
// compile with: /EHsc
#include <memory>
#include <iostream>
struct deleter
{
void operator()(int *p)
{
delete p;
}
};
int main()
{
std::weak_ptr<int> wp;
{
std::shared_ptr<int> sp(new int(10));
wp = sp;
std::cout << "wp.expired() == " << std::boolalpha
<< wp.expired() << std::endl;
std::cout << "*wp.lock() == " << *wp.lock() << std::endl;
}
// check expired after sp is destroyed
std::cout << "wp.expired() == " << std::boolalpha
<< wp.expired() << std::endl;
std::cout << "(bool)wp.lock() == " << std::boolalpha
<< (bool)wp.lock() << std::endl;
return (0);
}
Configuration requise
En-tête: <mémoire>
Espace de noms : std