[英]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.