|
Boost Users : |
From: Keith Weintraub (Keith_at_[hidden])
Date: 2005-11-16 10:17:07
Folks,
What is the best way to pass a
std::vector<boost::shared_ptr<Derived> > to a function that expects a
std::vector<boost::shared_ptr<Base> >?
Can I use a cast or should I create a new
std::vector<boost::shared_ptr<Base> > by iterating over the
std::vector<boost::shared_ptr<Derived> >?
Here is some sample code. What f is doing is not important it is just an
example:
class A { // This class is from a library I have no control over.
public:
virtual string getName() { return string("A");}
};
class B : public A { // I need to create B from A to have extra
functionality
public:
virtual string getName() { return string("B");}
virtual string getLongName() { return string("This is B");}
};
void f(std::vector<boost::shared_ptr<A> > &v) { // This function is
from a library that I have no control over.
for(int i = 0; i < v.size(); i++)
cout << v[i]->getName();
}
int main(char *argv, int arc) {
std::vector<boost::shared_ptr<B> > bvec(10);
boost::shared_ptr<B> bPtr(new B);
bvec[0] = bPtr;
f(bvec); // This should not compile. <-------------------------
}
The last line (with the arrow) won't compile because
std::vector<boost::shared_ptr<B> > cannot be converted to
std::vector<boost::shared_ptr<A> >.
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net