std::shared_ptr<const type>如何转型到std::shared_ptr<type>
c++吧
全部回复
仅看楼主
level 12
v2abcd
楼主
已尝试const_cast无效
强制转型会不会出问题
还是shared_ptr有const的版本?不过目前没有找到相关文档
2013年02月05日 05点02分
1
level 10
mypcluna
两个完全不相关的类,不能转。
2013年02月05日 05点02分
2
mypcluna
个人感觉没必要刻意追求什么s_p<const ...>,如有可能的话,把const拿掉吧。
2013年02月05日 06点02分
v2abcd
回复 mypcluna :呃
,只好用原始指针去了...
2013年02月05日 06点02分
level 12
Hope_20121221_
shared_ptr<type>(const_cast<type*>(csp.get()))这样?
2013年02月05日 06点02分
3
v2abcd
这样会破坏引用计数...
2013年02月05日 06点02分
mypcluna
危险啊……
2013年02月05日 06点02分
level 12
v2abcd
楼主
已解决...
std::const_pointer_cast
2013年02月05日 06点02分
4
v2abcd
还有dynamic_pointer_cast和static_pointer_cast对应其他两种转型
2013年02月05日 06点02分
mypcluna
奥对,有这么一个函数可用。不过我从不在智能指针里用const,觉得没必要。
2013年02月05日 06点02分
Hope_20121221_
原来有这种东西, 这才对嘛
2013年02月05日 06点02分
level 11
itianda
最近才知道const_cast/const_pointer_cast不仅能去const,还能加const
火星了
2013年02月05日 06点02分
5
1