Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/158.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 带循环引用的弱ptr的使用 所以我很难理解为什么我们必须使用弱小PTR ,特别是循环引用问题,请考虑这个代码: B类//远期申报 甲级{ 共享_ptr b_ptr; 公众: 无效集合(共享的ptr&B) { b_ptr=b; } A(){cout_C++_Shared Ptr_Weak Ptr - Fatal编程技术网

C++ 带循环引用的弱ptr的使用 所以我很难理解为什么我们必须使用弱小PTR ,特别是循环引用问题,请考虑这个代码: B类//远期申报 甲级{ 共享_ptr b_ptr; 公众: 无效集合(共享的ptr&B) { b_ptr=b; } A(){cout

C++ 带循环引用的弱ptr的使用 所以我很难理解为什么我们必须使用弱小PTR ,特别是循环引用问题,请考虑这个代码: B类//远期申报 甲级{ 共享_ptr b_ptr; 公众: 无效集合(共享的ptr&B) { b_ptr=b; } A(){cout,c++,shared-ptr,weak-ptr,C++,Shared Ptr,Weak Ptr,使用所有std::shared\u ptr,您有: int main() { std::shared_ptr<A> a = std::make_shared<A>(); // ref_count_a = 1 std::shared_ptr<B> b = std::make_shared<B>(); // ref_count_b = 1 a->set_B(b); // ref_count_b = 2 b->

使用所有
std::shared\u ptr
,您有:

int main() {
    std::shared_ptr<A> a = std::make_shared<A>(); // ref_count_a = 1
    std::shared_ptr<B> b = std::make_shared<B>(); // ref_count_b = 1
    a->set_B(b); // ref_count_b = 2
    b->set_A(a); // ref_count_a = 2
} // ref_count_a = 1 && ref_count_b = 1
// memleak due to the cycle
intmain(){
std::shared_ptr a=std::make_shared();//ref_count_a=1
std::shared_ptr b=std::make_shared();//ref_count_b=1
a->set_B(B);//ref_count_B=2
b->set_A(A);//ref_count_A=2
}//ref\u count\u a=1&&ref\u count\u b=1
//循环导致的内存泄漏

B类{
标准:弱ptr a ptr;
// ...
};
它变成:

int main() {
    std::shared_ptr<A> a = std::make_shared<A>(); // ref_count_a = 1
    std::shared_ptr<B> b = std::make_shared<B>(); // ref_count_b = 1
    a->set_B(b); // ref_count_b = 2
    b->set_A(a); // ref_count_a = 1 , weak_ref_a = 1
} // ref_count_a = 0 && ref_count_b = 1
// -> release a -> ref_count_b = 0
// -> release b (+ control block) -> weak_ref_a = 0
// -> release control block of a
intmain(){
std::shared_ptr a=std::make_shared();//ref_count_a=1
std::shared_ptr b=std::make_shared();//ref_count_b=1
a->set_B(B);//ref_count_B=2
b->set_A(A);//ref_count_A=1,弱_ref_A=1
}//ref\u count\u a=0&&ref\u count\u b=1
//->释放a->参考计数b=0
//->释放b(+控制块)->弱参考a=0
//->释放a的控制块
它还提到弱ptr破坏了强所有权引用,但是弱ptr怎么可能没有所有权,它将如何访问对象

控件为
共享\u ptr
维护一个计数器(以释放对象) 以及用于
弱\u ptr
的计数器,以释放控制块。 由于控制块,弱\u ptr检索共享\u ptr

最后我听说弱\u ptr是一个智能指针,它可以使用lock()或expired()等方法来管理共享\u ptr,这是否正确


是的

最后我听说一个弱的\u ptr是一个智能指针,可以使用lock()或expired()等方法来管理一个共享的\u ptr
,就像真的吗?@KamilCuk是的,我只是想确定一下,这样我的头脑中就有了一个清晰的图像,知道为什么要使用它们。
int main() {
    std::shared_ptr<A> a = std::make_shared<A>(); // ref_count_a = 1
    std::shared_ptr<B> b = std::make_shared<B>(); // ref_count_b = 1
    a->set_B(b); // ref_count_b = 2
    b->set_A(a); // ref_count_a = 1 , weak_ref_a = 1
} // ref_count_a = 0 && ref_count_b = 1
// -> release a -> ref_count_b = 0
// -> release b (+ control block) -> weak_ref_a = 0
// -> release control block of a