转换shared_ptr类型向量的迭代器
如何转换shared_ptr类型向量的迭代器?考虑以下示例:
typedef boost::shared_ptr < MyClass > type_myClass;
vector< type_myClass > vect;
vector< type_myClass >::iterator itr = vect.begin();
while(itr != vect.end())
{
//Following statement works, but I wish to rather cast this
//to MyClass and then call a function?
(*itr)->doSomething();
}
How can I cast an iterator of a vector of shared_ptr type? Consider following example:
typedef boost::shared_ptr < MyClass > type_myClass;
vector< type_myClass > vect;
vector< type_myClass >::iterator itr = vect.begin();
while(itr != vect.end())
{
//Following statement works, but I wish to rather cast this
//to MyClass and then call a function?
(*itr)->doSomething();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不想强制转换,而是提取对该对象的引用:
You do not want to cast, but rather extract a reference to the object:
您只需再次取消引用即可获取引用。
然后投射它或任何你想要的东西。
You can simply grab a reference by de-referencing it again.
And then cast it or whatever however you'd like.
您可以使用方法
get()
,根据文档:这意味着您可以执行以下操作:
You can use the method
get()
, according to the docs:It means you can do: