简体   繁体   English

来自weak_ptr的不安全原始指针-由上下文保证安全

[英]Unsafe raw pointer from weak_ptr - safety guaranteed by context

I am storing a weak_ptr for avoiding circular structure, and I know that in some particular context the shared_ptr is still valid. 我正在存储weak_ptr以避免循环结构,并且我知道在某些特定上下文中shared_ptr仍然有效。

Can I get the raw pointer from weak_ptr without casting to shared_ptr (which involves memory write)? 是否可以从weak_ptr获取原始指针而不转换为shared_ptr (涉及内存写入)?

(note: this is not a duplicate of How to maintain a weak pointer to a parent in C++? , I am specifically asking about an unsafe way, not about getting the pointer as such) (注意:这不是“ 如何在C ++中维护指向父代的弱指针?”的重复,我特别是在问一种不安全的方法,而不是在获取这样的指针)

Can I get the raw pointer from weak_ptr without casting to shared_ptr 我可以从weak_ptr获取原始指针而不转换为shared_ptr吗

No. 没有。

NB you don't "cast" you construct a shared_ptr from the weak_ptr . 注意,您不“投射”是从weak_ptr构造一个shared_ptr

(which involves memory write)? (这涉及内存写入)?

What do you mean here? 你这是什么意思 Creating a shared_ptr will increment a reference count, is that what you're referring to? 创建一个shared_ptr会增加一个引用计数,这就是您要引用的内容吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM