We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
错误题目,C++第 96 题 -- 智能指针的循环引用 原例子:
#include <iostream> using namespace std; template <typename T> class Node { public: Node(const T& value) :_pPre(NULL) , _pNext(NULL) , _value(value) { cout << "Node()" << endl; } ~Node() { cout << "~Node()" << endl; cout << "this:" << this << endl; } shared_ptr<Node<T>> _pPre; shared_ptr<Node<T>> _pNext; T _value; }; void Funtest() { shared_ptr<Node<int>> sp1(new Node<int>(1)); shared_ptr<Node<int>> sp2(new Node<int>(2)); cout << "sp1.use_count:" << sp1.use_count() << endl; cout << "sp2.use_count:" << sp2.use_count() << endl; sp1->_pNext = sp2; //sp1的引用+1 sp2->_pPre = sp1; //sp2的引用+1 cout << "sp1.use_count:" << sp1.use_count() << endl; cout << "sp2.use_count:" << sp2.use_count() << endl; } int main() { Funtest(); system("pause"); return 0; } //输出结果 //Node() //Node() //sp1.use_count:1 //sp2.use_count:1 //sp1.use_count:2 //sp2.use_count:2
其中对Next和Pre赋值处的注释有问题,正确为:
sp1->_pNext = sp2; //sp2的引用+1 sp2->_pPre = sp1; //sp1的引用+1
The text was updated successfully, but these errors were encountered:
已改正,谢谢指出错误!!! 详见:https://interviewguide.cn/#/Doc/Knowledge/C++/C++11%E6%96%B0%E6%A0%87%E5%87%86/C++11%E6%96%B0%E6%A0%87%E5%87%86?id=%e6%99%ba%e8%83%bd%e6%8c%87%e9%92%88%e7%9a%84%e5%be%aa%e7%8e%af%e5%bc%95%e7%94%a8
Sorry, something went wrong.
No branches or pull requests
错误题目,C++第 96 题 -- 智能指针的循环引用
原例子:
其中对Next和Pre赋值处的注释有问题,正确为:
The text was updated successfully, but these errors were encountered: