Actually, my problem is a bit more complicated than the subject gives away. I want to cast a vector< shared_ptr<Derived> > to a vector< shared_ptr<Base> >.
struct InternalManager {
vector< shared_ptr<XyzConnection> > ActiveConnections;
};
class PublicApi {
typedef vector< shared_ptr<Connection> > ConnectionVector;
const ConnectionVector &GetActiveConnections() const {
return InternalManager::ActiveConnections;
}
};
I know that technically, I could just reinterpret_cast<> the whole thing, but of course, that's a horrific way to solve my problem.
Another solution would be to write my own collection interface:
struct ConnectionList {
public: virtual size_t size() const = 0;
public: virtual shared_ptr<Connection> Get(size_t index) = 0;
};
struct XyzConnectionList : public ConnectionList {
public: virtual size_t size() const { return this->connections->size(); }
// Darn! Cannot use covariant returns due to shared_ptr<> here!
public: virtual shared_ptr<Connection> Get(size_t index) {
return this->connections.at(index);
}
public: virtual const shared_ptr<XyzConnection> &GetImpl(size_t index) {
return this->connections.at(index);
}
private: vector< shared_ptr<XyzConnection> > connections;
};
But maybe someone here knows a shorter way that doesn't require me to reimplement a full collection interface with iterators and stuff just to avoid copying a possibly large vector?
-Markus-