如何使用协变返回从基类检索派生类

How to use a covariant return to retrieve a derived class from a base class?

本文关键字:基类 检索 派生 返回 何使用      更新时间:2023-10-16

代码示例只是我实际程序的简单模型,该程序试图在基向量中保存来自单个基类的不同类。 然后使用虚函数调用来获取 *this,返回派生的。这样我就不需要多个容器了。

    #include "stdafx.h"
#include <iostream>
#include <vector>
class Base
{
public:
    virtual Base* getThis() { return this; }
    virtual void printClass() const { std::cout << "Base" << std::endl; }
};
class Derived : public Base
{
public:
    virtual Derived* getThis() { return this; }
    virtual void printClass() const { std::cout << "Derived" << std::endl; }
};

int main(int argc, _TCHAR* argv[])
{   
    Base Bar;
    Derived Foo;
    typedef std::vector<Base*> vContainer;
    vContainer Objects;
    Objects.push_back(new Derived);
    for (vContainer::iterator it = Objects.begin(); it != Objects.end(); ++it)
    {
        Bar = **it; // works to get Base but not wanted
        // attempts     
        //Foo = it->getThis(); // the pointer selector doesnt work...
        //Foo = static_cast<Derived>(**it);  // utterly confused!       
    }
    Bar.printClass(); // prints base as expected
    //Foo.printClass();  // Mean't to print Derived
    std::cin.get();
    return 0;
}

几个小时以来,我一直在寻找对此的更好理解,但每个人都只是谈论克隆,这不是我所追求的。任何帮助将不胜感激。

尼尔

为了安全起见,请使用 dynamic_cast .

for (vContainer::iterator it = Objects.begin(); it != Objects.end(); ++it)
{
    Bar* basePtr = *it;
    Derived* derivedPtr = dynamic_cast<Derived*>(basePtr);
    if ( derivedPtr ) // Check whether the dynamic_cast was successful.
    {
       // Use derivedPtr
    }
}

你的代码

Bar = **it; // works to get Base but not wanted

不会从向量获取对象到条形图。这只是与这个几乎相同的分配(添加了一些输出):

class Base
{
public:
    virtual Base* getThis() { return this; }
    virtual void printClass() const { std::cout << "Base" << std::endl; }
    Base& operator=(Base& one) { std::cout << "operator = is working" << std::endl; return *this;}
};

所以,如果你想有指向对象的指针存储在矢量中,不要尝试复制对象,复制指针(*迭代器)。

您要实现的是方法的动态绑定,而不管静态类型如何,这是虚拟方法的全部目的。您所要做的就是在容器中使用指向这些对象的指针或引用。请看下面的代码:

#include <iostream>
#include <vector>
class Base
{
public:
    virtual void printClass() const { std::cout << "Base" << std::endl; }
};
   class Derived : public Base
{
public:
    Derived(){};
    virtual void printClass() const { std::cout << "Derived" << std::endl; }
};

int main()
{   
    typedef std::vector<Base*> vContainer;
    vContainer Objects;
    Objects.push_back(new Base);
    Objects.push_back(new Derived);

    for (vContainer::iterator it = Objects.begin(); it != Objects.end(); ++it)
    {
         // prints Base on 1st iteration
         // prints Derived on 2nd iteration
        (*it)->printClass();
   }
    return 0;
}

您的尝试失败的原因是 Bar 是一个局部变量,而不是引用/指针。这意味着内存中 Bar 的大小是在编译时确定的,并且是 sizeof(Base)。将派生对象分配给它将按值复制对象,并自动删除存储在派生对象中的额外信息,并将其转换为 Base 对象(额外的信息根本无法存储在该数量的内存中)。如果 bar 的类型为 Base* 并且您将其指向 Derived 对象,则 Bar->printClass() 将打印 Derived。

希望能澄清它。