<tfoot id='FcRJ6'></tfoot>
    1. <legend id='FcRJ6'><style id='FcRJ6'><dir id='FcRJ6'><q id='FcRJ6'></q></dir></style></legend>

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

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

      1. C++ 转换为派生类

        时间:2023-08-02
        • <bdo id='jzi02'></bdo><ul id='jzi02'></ul>

              <tfoot id='jzi02'></tfoot>

                  <legend id='jzi02'><style id='jzi02'><dir id='jzi02'><q id='jzi02'></q></dir></style></legend>

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

                    <tbody id='jzi02'></tbody>
                • <i id='jzi02'><tr id='jzi02'><dt id='jzi02'><q id='jzi02'><span id='jzi02'><b id='jzi02'><form id='jzi02'><ins id='jzi02'></ins><ul id='jzi02'></ul><sub id='jzi02'></sub></form><legend id='jzi02'></legend><bdo id='jzi02'><pre id='jzi02'><center id='jzi02'></center></pre></bdo></b><th id='jzi02'></th></span></q></dt></tr></i><div id='jzi02'><tfoot id='jzi02'></tfoot><dl id='jzi02'><fieldset id='jzi02'></fieldset></dl></div>
                  本文介绍了C++ 转换为派生类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  如何转换为派生类?以下方法都给出了以下错误:

                  How can i cast to a derived class? The below approaches all give the following error:

                  无法从 BaseType 转换为 DerivedType.没有构造函数可以接受源类型或构造函数重载解析不明确.

                  Cannot convert from BaseType to DerivedType. No constructor could take the source type, or constructor overload resolution was ambiguous.

                  BaseType m_baseType;
                  
                  DerivedType m_derivedType = m_baseType; // gives same error
                  
                  DerivedType m_derivedType = (DerivedType)m_baseType; // gives same error
                  
                  DerivedType * m_derivedType = (DerivedType*) & m_baseType; // gives same error
                  

                  推荐答案

                  这样想:

                  class Animal { /* Some virtual members */ };
                  class Dog: public Animal {};
                  class Cat: public Animal {};
                  
                  
                  Dog     dog;
                  Cat     cat;
                  Animal& AnimalRef1 = dog;  // Notice no cast required. (Dogs and cats are animals).
                  Animal& AnimalRef2 = cat;
                  Animal* AnimalPtr1 = &dog;
                  Animal* AnimlaPtr2 = &cat;
                  
                  Cat&    catRef1 = dynamic_cast<Cat&>(AnimalRef1);  // Throws an exception  AnimalRef1 is a dog
                  Cat*    catPtr1 = dynamic_cast<Cat*>(AnimalPtr1);  // Returns NULL         AnimalPtr1 is a dog
                  Cat&    catRef2 = dynamic_cast<Cat&>(AnimalRef2);  // Works
                  Cat*    catPtr2 = dynamic_cast<Cat*>(AnimalPtr2);  // Works
                  
                  // This on the other hand makes no sense
                  // An animal object is not a cat. Therefore it can not be treated like a Cat.
                  Animal  a;
                  Cat&    catRef1 = dynamic_cast<Cat&>(a);    // Throws an exception  Its not a CAT
                  Cat*    catPtr1 = dynamic_cast<Cat*>(&a);   // Returns NULL         Its not a CAT.
                  

                  现在回顾你的第一句话:

                  Now looking back at your first statement:

                  Animal   animal = cat;    // This works. But it slices the cat part out and just
                                            // assigns the animal part of the object.
                  Cat      bigCat = animal; // Makes no sense.
                                            // An animal is not a cat!!!!!
                  Dog      bigDog = bigCat; // A cat is not a dog !!!!
                  

                  您应该很少需要使用动态转换.
                  这就是为什么我们有虚方法:

                  You should very rarely ever need to use dynamic cast.
                  This is why we have virtual methods:

                  void makeNoise(Animal& animal)
                  {
                       animal.DoNoiseMake();
                  }
                  
                  Dog    dog;
                  Cat    cat;
                  Duck   duck;
                  Chicken chicken;
                  
                  makeNoise(dog);
                  makeNoise(cat);
                  makeNoise(duck);
                  makeNoise(chicken);
                  

                  我能想到的唯一原因是,如果您将对象存储在基类容器中:

                  The only reason I can think of is if you stored your object in a base class container:

                  std::vector<Animal*>  barnYard;
                  barnYard.push_back(&dog);
                  barnYard.push_back(&cat);
                  barnYard.push_back(&duck);
                  barnYard.push_back(&chicken);
                  
                  Dog*  dog = dynamic_cast<Dog*>(barnYard[1]); // Note: NULL as this was the cat.
                  

                  但是如果您需要将特定对象转换回 Dogs,那么您的设计就存在一个基本问题.您应该通过虚拟方法访问属性.

                  But if you need to cast particular objects back to Dogs then there is a fundamental problem in your design. You should be accessing properties via the virtual methods.

                  barnYard[1]->DoNoiseMake();
                  

                  这篇关于C++ 转换为派生类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:C++虚函数返回类型 下一篇:为什么在虚拟继承中调用默认构造函数?

                  相关文章

                  最新文章

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

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

                    <bdo id='BycaA'></bdo><ul id='BycaA'></ul>

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