简体   繁体   English

具有派生对象的基类的std :: vector

[英]std::vector of Base class with Derived objects

I have a deque of std::shared_ptr declared has a deque of a base class (let's call it Polygon) but I'm storing on it pointers to derived objecs (for example Triangle). 我有一个std::shared_ptr的双端队列声明有一个基类的双端队列(我们称其为Polygon),但是我在其上存储了指向派生objecs的指针(例如Triangle)。 I'm thinking now about change the data structure to be a std::vector to use the fact that vector ensures adjacent memory but I have a couple of questions here: 我现在正在考虑将数据结构更改为std::vector以利用vector确保相邻内存的事实,但是我在这里有两个问题:

1) Is this possible even if the objects have different sizes? 1)即使物体的大小不同,这也是可能的吗? Or can happen that due to the derived objects are bigger than the base one they are going to overlap in memory? 还是会发生这样的情况:由于派生的对象比基对象大,它们将在内存中重叠?

2) Storing std::shared_ptr I'm not going to have them literally together in memory just the pointers is it true? 2)存储std::shared_ptr我不会将它们从字面上直接存储在内存中,只是指针是真的吗? Or am I wrong? 还是我错了?

Thanks 谢谢

Is this possible even if the objects have different sizes? 即使物体的大小不同,这是否可能? Or can happen that due to the derived objects are bigger than the base one they are going to overlap in memory? 还是会发生这样的情况:由于派生的对象比基对象大,它们将在内存中重叠?

This is fine. 这可以。 Even though Derived can be larger than Base you aren't actually storing those in the vector. 即使“ Derived可以大于“ Base您实际上并没有将它们存储在向量中。 You are storing a pointer to them and the pointer is always the same size. 您正在存储指向它们的指针,并且指针的大小始终相同。

Storing std::shared_ptr I'm not going to have them literally together in memory just the pointers is it true? 存储std::shared_ptr我不会将它们从字面上直接存储在内存中,只是指针是真的吗? Or am I wrong? 还是我错了?

Correct, the pointers will sit next to each other in the vector, but what they point to could be anywhere in memory. 正确,指针将在向量中彼此相邻,但它们指向的内容可能在内存中的任何位置。

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

相关问题 基本类对象的c ++ std :: vector-派生类的运行方法 - c++ std::vector of base class objects -— running method of derived class 如何默认初始化std :: vector <Base> 基本对象和派生对象? - How to default-initialize std::vector<Base> with Base and Derived objects? std :: vector保存基类实例,而不是派生类的实例 - std::vector holds base class instance and not of a derived class 创建基类对象的向量并将派生类对象存储在其中 - Create a vector of base class objects and store derived class objects within 如何在指向基类对象的指针向量中引用派生对象? - How to reference derived objects in a vector of pointers to base class objects? 使指向基类指针std :: vector的指针指向派生类指针的std :: vector - Making a pointer to std::vector of base class pointers point to std::vector of derived class pointers 使用基本接口类参数将派生类对象添加到向量 - Adding derived class objects to vector with base interface class parameter 派生类对象在对象向量中覆盖基类的函数 - Derived class object override a function of base class in a vector of objects 将抽象派生类对象存储在基类向量中 - Store abstract derived class objects in base class vector 制作基类指针的向量并将派生类对象传递给它(多态) - Making a vector of base class pointers and pass Derived class objects to it (Polymorphism)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM