如何使用协变返回从基类检索派生类
How to use a covariant return to retrieve a derived class from a base class?
代码示例只是我实际程序的简单模型,该程序试图在基向量中保存来自单个基类的不同类。 然后使用虚函数调用来获取 *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。
希望能澄清它。
相关文章:
- std::具有相同基类的类的变体
- 是否可以初始化不可复制类型的成员变量(或基类)
- 在C++中,是否可以基于给定的标识符创建基类的新实例,反之亦然
- 基类中的函数名称解析
- C++初始化基类
- 如何通过派生类函数更改基类中的向量
- 如何定义一个纯抽象基类
- 如何使用基类指针引用派生类成员
- 继承:构造函数,初始化C++11中基类的类C数组成员
- 使用基类指针创建对象时,缺少派生类析构函数
- 如何引用基类的派生类?
- 如果基类包含双指针成员,则派生类的构造函数
- 在模板基类中为继承类中的可选重写生成虚拟方法
- 为什么此派生对象无法访问基类的后递减方法?
- 公开最直接的基类模板名称
- 当基类是依赖类型时,这是一个缺陷吗
- 如何使基类的运算符对基类的可变参数数可见(请参阅下面的代码)?
- 模板基类中的静态变量
- 从Antlrcpp :: any中检索基类
- 如何使用协变返回从基类检索派生类