小编nah*_*dev的帖子

c++ std::vector<NotAPointer> 如何存储不同大小的对象,以及当它不包含指针时 ++it 如何知道跳转到哪里

编辑:TLDR;我是对象切片的受害者,但我对此一无所知。现在原来的问题如下。

我试图了解std::vector<MyClass>当 MyDerived 的实例被push_backed到其中时如何存储对象。另外,迭代器如何知道下一个内存块的开始位置,以便增量++运算符知道如何到达那里。考虑以下代码示例:

#include <iostream>
#include <vector>
using namespace std;

class BaseShape
{
public:
    // BaseShape() { cout << "BaseShape() "; }

    virtual void draw() const { cout << "BASE?\n"; }
};

class Circle : public BaseShape
{
public:
    Circle() { cout << "Circle()"; }

    virtual void draw() const override { cout << "Circle!\n"; }

    void *somePointer, *ptr2;
};

class Triangle : public BaseShape
{
public:
    Triangle() { cout << "Triangle()"; }

    virtual …
Run Code Online (Sandbox Code Playgroud)

c++ pointers iterator

1
推荐指数
1
解决办法
63
查看次数

标签 统计

c++ ×1

iterator ×1

pointers ×1