简体   繁体   English

我怎样才能拥有一个基本 class 对象的列表并访问派生的 class 字段?

[英]How can I have a list of base class objects and access derived class fields?

Let's say I have a base class and two derived classes:假设我有一个基类 class 和两个派生类:

class a {};
class b : a { int a; };
class c : a { int b; };

And I have a list of the base class, and I insert the derived class into the list:我有一个基数 class 的列表,我将派生的 class 插入到列表中:

std::list<a> list;

list.emplace_back(b());
list.emplace_back(c());

Now, I want to access int a like this:现在,我想像这样访问int a

for(auto i : list)
{
    i.a = 5;
}

I already have checks in place to see what class it is, but the compiler is still not letting me access it.我已经检查过它是什么 class,但编译器仍然不允许我访问它。

How can I access any field in the derived class from the base class?如何从基数 class 访问派生的 class 中的任何字段?

This question has been asked many times, but none of the methods have worked for me so far.这个问题已被问过很多次,但到目前为止,没有一种方法对我有用。

First, you need a list of base class pointers , otherwise you will slice the objects when you insert them into the list.首先,您需要一个基数为 class 的指针列表,否则当您将对象插入列表时,您将对对象进行切片

Then, you need to type-cast the pointers to access derived classes as needed.然后,您需要对指针进行类型转换以根据需要访问派生类。

If all of the list elements are pointing at the same derived type, you can use static_cast :如果所有列表元素都指向相同的派生类型,您可以使用static_cast

struct A {};
struct B : A { int a; };
struct C : A { int b; };

std::list<std::unique_ptr<A>> lst;

lst.push_back(std::make_unique<B>());
lst.push_back(std::make_unique<B>());

for(auto &ptr : lst)
{
    static_cast<B*>(ptr.get())->a = 5;
}

Otherwise, use dynamic_cast instead to test the derived class type before accessing its fields:否则,在访问其字段之前使用dynamic_cast来测试派生的 class 类型:

struct A {};
struct B : A { int a; };
struct C : A { int b; };

std::list<std::unique_ptr<A>> lst;

lst.push_back(std::make_unique<B>());
lst.push_back(std::make_unique<C>());

for(auto &ptr : lst)
{
    B *b = dynamic_cast<B*>(ptr.get());
    if (b)
        b->a = 5;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我有一个基础对象和派生类对象的向量,但是我无法访问由存储在向量中的派生对象继承的数据成员 - I have a vector of Base and Derived Class Objects, but I can't access data members inherited by derived objects that are stored in the vector 如果将派生类的方法作为基类返回,该如何访问派生类的方法? - How can I access a derived class' methods if it is returned as the base class? 如何让派生类对象访问基类中的值? - How to have a derived class object access values from the base class? 如何将派生类的对象存储在可以访问基本 class 没有的属性的数据结构中? - How can I store objects of derived classes within a data structure from which can be accessed attributes which the base class does not have? 有没有一种方法可以使用派生类访问基类中的值? - Is there a way that I can access the values in the base class using the derived class? 如何从派生类访问派生基成员?(在C ++中) - How can I access derived base member from derived class?(in C++) 如何在不直接继承的情况下访问基类容器中的派生对象 - How to access derived objects in base class container without direct inheritance 基类的好友类如何通过从基类派生的类的对象访问该基类的成员? - How does friend class of the base class access members of that base class through objects of class derived from the base class? 我怎样才能让派生类的对象“引用”基类实例的成员? - How can I have an object of a derived class “reference” members of an instance of the base class? 如何调用基类的虚拟函数定义,该基类在C ++中的抽象基类和派生类中均具有定义? - How can I call virtual function definition of base class that have definitions in both abstract base class and derived class in C++?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM