如何使用协变 return 从基数 class 中检索派生的 class?

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

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

    #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

没有从 vector 获取对象到 Bar。这只是与这个几乎相同的分配(添加了一些输出):

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;}
};

因此,如果您希望将指向对象的指针存储在向量中,请不要尝试复制对象,而是复制指针 (*iterator)。

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

#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 是局部变量而不是 reference/pointer。这意味着 Bar 在内存中的大小是在编译时确定的,并且是 sizeof(Base)。将 Derived 对象分配给它会按值复制对象并自动删除存储在 Derived 对象中的额外信息并将其变成 Base 对象(额外信息根本无法存储在该内存量中)。如果 bar 是 Base* 类型,并且您让它指向 Derived 对象,那么 Bar->printClass() 将打印 Derived.

希望澄清一下。