intCreateUser(constData& data,std::weak_ptr<CPlayer> player){if(player.expired()) {return-1; }std::shared_ptr<CPlayer> playerPtr = player.lock();if(playerPtr->GetSocket().expired()) {return-1; }if(!playerPtr->GetSocket().lock()->GetPlayer().expired()) { PBS2CCreateUserRes res...
// std_tr1__memory__weak_ptr_expired.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...
std::shared_ptr<int>sp1(newint(22));std::shared_ptr<int>sp2=sp1;std::weak_ptr<int>wp=sp1;// point to sp1std::cout<<wp.use_count()<<std::endl;// 2if(!wp.expired()){std::shared_ptr<int>sp3=wp.lock();std::cout<<*sp3<<std::endl;// 22} 2.3 由std::weak_ptr构造std::...
weak_ptr::expired判断shared_ptr是否有效,即shared_ptr内部数据是否被释放 weak_ptr是否可以监看shared_ptr中的内部数据呢?因为weak_ptr是弱指针,所以不能直接访问,但是可以通过weak_ptr::lock间接访问。 weak_ptr::lock weak_ptr::lock返回构造weak_ptr的shared_ptr,当shared_ptr已经被释放时,返回的是空shared_...
static_pointer_cast Function swap Function weak_ptr Class weak_ptr Class weak_ptr::element_type weak_ptr::expired weak_ptr::lock weak_ptr::operator= weak_ptr::reset weak_ptr::swap weak_ptr::use_count weak_ptr::weak_ptr _DO_NOT_DECLARE_INTERLOCKED_INTRINSICS_IN_MEMORY MacroLearn...
#include <iostream> #include <memory> std::weak_ptr<int> gw; void f() { if (!gw.expired()) std::cout << "gw 有效\n"; else std::cout << "gw 已过期\n"; } int main() { { auto sp = std::make_shared<int>(42); gw = sp; f(); } f(); } 输出: gw 有效 gw 已...
#include <iostream>#include <memory>std::weak_ptr<int>gw;voidf(){if(!gw.expired()){std::cout<<"gw is valid\n";}else{std::cout<<"gw is expired\n";}}intmain(){{autosp=std::make_shared<int>(42);gw=sp;f();}f();} ...
#include <iostream> #include <memory> std::weak_ptr<int> gw; void f() { if (!gw.expired()) { std::cout << "gw is valid\n"; } else { std::cout << "gw is expired\n"; } } int main() { { auto sp = std::make_shared<int>(42); gw = sp; f(); } f(); } 输出...
1. 直接初始化:weak_ptr<T> wp(sp); //其中sp为shared_ptr类型 2. 赋值: wp1 = sp; //其中sp为shared_ptr类型 wp2 = wp1; //其中wp1为weak_ptr类型 (二)常用操作 1. use_count():获取当前控制块中资源的强引用计数。 2. expired():判断所观测的资源是否失效(即己经被释放),即use_count是否...
swap(x) 其中 x 表示一个同类型的 weak_ptr 类型指针,该函数可以互换 2 个同类型 weak_ptr 指针的内容。 reset() 将当前 weak_ptr 指针置为空指针。 use_count() 查看指向和当前 weak_ptr 指针相同的 shared_ptr 指针的数量。 expired() 判断当前 weak_ptr 指针为否过期(指针为空,或者指向的堆内存已经...