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();
}
You do not want to cast, but rather extract a reference to the object:
MyClass & obj = *(*it); // dereference iterator, dereference pointer
obj.doSomething();
You can simply grab a reference by de-referencing it again.
MyClass& ref = **itr;
And then cast it or whatever however you'd like.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With