<legend id='JZQNt'><style id='JZQNt'><dir id='JZQNt'><q id='JZQNt'></q></dir></style></legend>
    <i id='JZQNt'><tr id='JZQNt'><dt id='JZQNt'><q id='JZQNt'><span id='JZQNt'><b id='JZQNt'><form id='JZQNt'><ins id='JZQNt'></ins><ul id='JZQNt'></ul><sub id='JZQNt'></sub></form><legend id='JZQNt'></legend><bdo id='JZQNt'><pre id='JZQNt'><center id='JZQNt'></center></pre></bdo></b><th id='JZQNt'></th></span></q></dt></tr></i><div id='JZQNt'><tfoot id='JZQNt'></tfoot><dl id='JZQNt'><fieldset id='JZQNt'></fieldset></dl></div>

    <small id='JZQNt'></small><noframes id='JZQNt'>

      • <bdo id='JZQNt'></bdo><ul id='JZQNt'></ul>

    1. <tfoot id='JZQNt'></tfoot>

    2. 尝试从 Derived* 的向量中分配 Base* 的向量

      Trying to assign vector of Base* from vector of Derived*(尝试从 Derived* 的向量中分配 Base* 的向量)

      <i id='KQ4Ax'><tr id='KQ4Ax'><dt id='KQ4Ax'><q id='KQ4Ax'><span id='KQ4Ax'><b id='KQ4Ax'><form id='KQ4Ax'><ins id='KQ4Ax'></ins><ul id='KQ4Ax'></ul><sub id='KQ4Ax'></sub></form><legend id='KQ4Ax'></legend><bdo id='KQ4Ax'><pre id='KQ4Ax'><center id='KQ4Ax'></center></pre></bdo></b><th id='KQ4Ax'></th></span></q></dt></tr></i><div id='KQ4Ax'><tfoot id='KQ4Ax'></tfoot><dl id='KQ4Ax'><fieldset id='KQ4Ax'></fieldset></dl></div>
      <legend id='KQ4Ax'><style id='KQ4Ax'><dir id='KQ4Ax'><q id='KQ4Ax'></q></dir></style></legend>

            <tbody id='KQ4Ax'></tbody>
            <bdo id='KQ4Ax'></bdo><ul id='KQ4Ax'></ul>
          • <tfoot id='KQ4Ax'></tfoot>

              <small id='KQ4Ax'></small><noframes id='KQ4Ax'>

              • 本文介绍了尝试从 Derived* 的向量中分配 Base* 的向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                这似乎是一个非常基本的问题,但我无法弄清楚.我有一个 std::vector 指向派生对象的原始指针,我只想使用赋值运算符将它复制到另一个 Base 指针向量.使用 VC++ 我得到错误 C2679二进制'=:没有找到运算符..."顺便说一句,我不想复制对象的深层副本,我只想复制指针.示例代码:

                This seems like a pretty basic problem, but I can't figure it out. I have a std::vector of raw pointers to Derived objects, and I just want to copy it to another vector of Base pointers using the assignment operator. With VC++ I get error C2679 "binary '=': no operator found..." BTW I don't want a deep copy of the objects, I just want to copy the pointers. Sample code:

                #include <vector>
                using namespace std;
                
                struct Base{};    
                struct Derived: public Base {};
                
                int main (int argc, char* argv[])
                {
                    vector<Derived*> V1;
                    vector<Base*> V2;
                    V2 = V1;  //Compiler error here
                    return 0;
                }
                

                令我困惑的是,我可以通过循环遍历向量并使用 push_back 来复制向量,如下所示:

                What confuses me is that I can copy the vector by looping through it and using push_back, like this:

                for (Derived* p_derived : V1)
                    V2.push_back(p_derived);
                

                所以我的问题是为什么分配失败,而 push_back 有效?对我来说似乎是一样的.

                So my question is why does the assignment fail, while push_back works? Seems like the same thing to me.

                推荐答案

                那是因为 BaseDerived 有关系,vector< 没有关系;Base*>vector.就类层次结构而言,它们完全不相关,因此您不能将一个分配给另一个.

                That's because while Base and Derived have a relationship, there is no relationship between vector<Base*> and vector<Derived*>. As far as class hierarchy is concerned, they are entirely unrelated, so you can't assign one to the other.

                您正在寻找的概念称为协方差.例如,在 Java 中,String[]Object[] 的子类型.但是在 C++ 中,这两种类型只是不同的类型,与 String[]Bar 没有什么关系.

                The concept you are looking for is called covariance. In Java for instance, String[] is a subtype of Object[]. But in C++, these two types are just different types and are no more related than String[] and Bar.

                push_back 有效,因为该方法只需要一个 T const&(或 T&&),所以任何可以转换为 Base* 是可以接受的 - Derived* 是.

                push_back works because that method just takes a T const& (or T&&), so anything convertible to a Base* will be acceptable - which a Derived* is.

                也就是说,vector 有一个带有一对迭代器的构造函数,在这里应该更容易使用:

                That said, vector has a constructor that takes a pair of iterators, which should be easier to use here:

                vector<Base*> v2(v1.begin(), v1.end());
                

                或者,因为它已经构建:

                Or, since it is already constructed:

                v2.assign(v1.begin(), v1.end());
                

                这篇关于尝试从 Derived* 的向量中分配 Base* 的向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Consistent pseudo-random numbers across platforms(跨平台一致的伪随机数)
                Vary range of uniform_int_distribution(改变uniform_int_distribution的范围)
                What is a seed in terms of generating a random number?(就生成随机数而言,种子是什么?)
                Is 1.0 a valid output from std::generate_canonical?(1.0 是 std::generate_canonical 的有效输出吗?)
                Getting big random numbers in C/C++(在 C/C++ 中获取大随机数)
                What is the best way to generate random numbers in C++?(在 C++ 中生成随机数的最佳方法是什么?)

                    <tbody id='O9QsN'></tbody>

                  <small id='O9QsN'></small><noframes id='O9QsN'>

                        <legend id='O9QsN'><style id='O9QsN'><dir id='O9QsN'><q id='O9QsN'></q></dir></style></legend>
                        <tfoot id='O9QsN'></tfoot>

                        • <bdo id='O9QsN'></bdo><ul id='O9QsN'></ul>

                        • <i id='O9QsN'><tr id='O9QsN'><dt id='O9QsN'><q id='O9QsN'><span id='O9QsN'><b id='O9QsN'><form id='O9QsN'><ins id='O9QsN'></ins><ul id='O9QsN'></ul><sub id='O9QsN'></sub></form><legend id='O9QsN'></legend><bdo id='O9QsN'><pre id='O9QsN'><center id='O9QsN'></center></pre></bdo></b><th id='O9QsN'></th></span></q></dt></tr></i><div id='O9QsN'><tfoot id='O9QsN'></tfoot><dl id='O9QsN'><fieldset id='O9QsN'></fieldset></dl></div>