adambard / adambard/learnxinyminutes-docs
[c++] std::shared_ptr circular reference example incorrect?
- Ngôn ngữ chính
- Markdown
- Star
- 12.3k
- Fork
- 3.7k
- Merge trung bình
- 13 giờ 10 phút
- Pull request đã merge (30 ngày)
- 6
Mô tả
https://learnxinyminutes.com/docs/c++/
I do not think the example creates or exemplifies circular pointers.
```
// Beware of possible circular references!!!
// There will be always a reference, so it will be never destroyed!
std::shared_ptr doggo_one(new Dog());
std::shared_ptr doggo_two(new Dog());
doggo_one = doggo_two; // p1 references p2
doggo_two = doggo_one; // p2 references p1
```
The `doggo_one`/`doggo_two` manipulation just looks like it changes which `Dog` instance the smart pointers refers to, without creating any cycles.
I do not know C++ (that's why I'm here) but my guess is that circular (smart) pointers would require, e.g., that each `Dog` object to contain a `std::shared_ptr` to the other `Dog` object.
Hướng dẫn đóng góp
Đánh giá
Issue này chưa được đánh giá.