简体   繁体   English

C ++转换std :: vector <Derived> *到std :: vector <Base> ……?

[英]C++ casting a std::vector<Derived>* to std::vector<Base> …?

I'm making a vector of pointers to vectors: 我正在制作指向向量的指针的向量:

    std::vector<std::vector<Base>*> all; // vector keeping pointers to vectors

and the problem is I have several vectors of derived classes: 问题是我有几个派生类的向量:

    std::vector<Derived1> derived1; // vector of derived class
    std::vector<Derived2> derived2; // vector of derived class
    std::vector<Derived3> derived2; // vector of derived class

Is there any safe way to change the type of a pointer from vector<DerivedX> to vector<Base> ? 有什么安全的方法可以将指针的类型从vector<DerivedX>更改为vector<Base>吗?

Every derived class shares certain behavior and they will be alive for most of the program's life. 每个派生类都具有某些行为,并且在程序的大部分时间内都将存活。

I'm still not good at explaining my programming problems, should any questions concerning details arise - please ask in comments. 如果出现有关细节的任何问题,我仍然不能很好地解释我的编程问题-请在评论中提问。

No, that is not possible. 不,那是不可能的。 There is no relationship between std::vector<Base> and std::vector<Derived> . std::vector<Base>std::vector<Derived>之间没有关系。

In other words: std::vector<Derived> is NOT derived from std::vector<Base> . 换句话说: std::vector<Derived>不是从std::vector<Base>派生的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM