weak_ptr::swap
交換兩個 weak_ptr 物件。
void swap(weak_ptr& wp);
參數
- wp
交換的弱式指標。
備註
成員函式讓資源一開始指向 *this 後續要求的 wp和資源一開始指向 wp 後續要求的 *this。函式不會變更兩個資源的參考計數,並且不會擲回任何例外狀況。
範例
// std_tr1__memory__weak_ptr_swap.cpp
// compile with: /EHsc
#include <memory>
#include <iostream>
struct deleter
{
void operator()(int *p)
{
delete p;
}
};
int main()
{
std::shared_ptr<int> sp1(new int(5));
std::shared_ptr<int> sp2(new int(10));
std::cout << "*sp1 == " << *sp1 << std::endl;
sp1.swap(sp2);
std::cout << "*sp1 == " << *sp1 << std::endl;
swap(sp1, sp2);
std::cout << "*sp1 == " << *sp1 << std::endl;
std::cout << std::endl;
std::weak_ptr<int> wp1(sp1);
std::weak_ptr<int> wp2(sp2);
std::cout << "*wp1 == " << *wp1.lock() << std::endl;
wp1.swap(wp2);
std::cout << "*wp1 == " << *wp1.lock() << std::endl;
swap(wp1, wp2);
std::cout << "*wp1 == " << *wp1.lock() << std::endl;
return (0);
}
需求
標題: <memory>
命名空間: std