对象向量

发布于 2024-08-16 09:30:03 字数 687 浏览 3 评论 0原文

我尝试将对象添加到“内容”向量,并对所有对象使用 show() 。 但是作为“Base”的子对象(A,B)的行为就像它们是“Base”类型一样, 这不是我的本意。看来,我尝试使用虚拟函数,但它不起作用。

我希望代码能说明一切。

class Base {
    public:
        virtual void show() { cout << "Base "; }
};

class A : public Base {
    public:
        virtual void show() { cout << "A "; }
};

class B : public Base {
    public:
        virtual void show() { cout << "B"; }
};



vector<Base> content;

void add(Base &o) {
    content.push_back(o);
}

A test1;
B test2;

add(test1);
add(test2);

for (size_t i = 0; i < content.size(); i++) {
        collection[i].show(); // output is: Base Base instead of A B
}   

提前致谢。

I tried to add objects to the "content" vector, and use show() on all of them.
But the objects that are children (A, B) of "Base" behave like they were of "Base" type,
what is not my intention. As it seems, I tried to use virtual functions but it doesn't work.

I hope that the code will speak for itself.

class Base {
    public:
        virtual void show() { cout << "Base "; }
};

class A : public Base {
    public:
        virtual void show() { cout << "A "; }
};

class B : public Base {
    public:
        virtual void show() { cout << "B"; }
};



vector<Base> content;

void add(Base &o) {
    content.push_back(o);
}

A test1;
B test2;

add(test1);
add(test2);

for (size_t i = 0; i < content.size(); i++) {
        collection[i].show(); // output is: Base Base instead of A B
}   

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

断爱 2024-08-23 09:30:04

STL容器存储值对象。这里的内容称为切片

STL containers store value object. What you have here is called slicing.

望喜 2024-08-23 09:30:04
vector<Base*> content; // <<

void add(Base *o) { // <<
    content.push_back(o);
}

A test1;
B test2;

add(&test1); // <<
add(&test2); // <<

for (size_t i = 0; i < content.size(); i++) {
        collection[i]->show(); // <<
}   
vector<Base*> content; // <<

void add(Base *o) { // <<
    content.push_back(o);
}

A test1;
B test2;

add(&test1); // <<
add(&test2); // <<

for (size_t i = 0; i < content.size(); i++) {
        collection[i]->show(); // <<
}   
甜是你 2024-08-23 09:30:04

您必须使用 vector 因为目前您在 add 中传递的对象将复制Base 类型的新对象中。

You have to use vector<Base*> because currently the objects that you pass in add will be copied into a new object of type Base.

送君千里 2024-08-23 09:30:03

你拥有的是一个 Base 向量。
当你向向量添加一些东西时,它会被复制到向量中,但只有基础部分被复制。因此,您将丢失所有派生信息。

这通常称为切片问题。

一个简单的解决方案是将指针存储在向量中。

What you have is a vector of Base.
When you add somthing to a vector it is copied into the vector But only the baser part is being copied. Thus you will loose all the dertived information.

This is cooqually refered to as the slicing problem.

A simple solution is to store pointers in the vector.

话少情深 2024-08-23 09:30:03

正如其他人所说,您正在经历切片。 向量存储一个Base,任何派生信息都会被分割掉。

用指针缓解这个问题:

std::vector<Base*> v;
v.push_back(new A);
v.push_back(new B);

现在的问题是释放你的资源。在向量超出范围之前,您需要迭代它并删除每个元素。更糟糕的问题是例外情况。

如果在向量生命周期内的任何时候抛出异常,它将展开堆栈,在某个时刻释放向量中的所有指针;一个巨大的内存泄漏。

如果使用更专用的容器,就会避免这个问题。要么自己写一个(当然不推荐),要么使用 增强指针容器

As others have said, you're experiencing slicing. The vector stores a Base, and any derived information gets sliced away.

Alleviate this with pointers:

std::vector<Base*> v;
v.push_back(new A);
v.push_back(new B);

The problem now is with releasing your resources. Before the vector goes out of scope, you'll need to iterate through it and delete each element. An even worse problem is with exceptions.

If at any point during the life-time of the vector, if an exception is thrown, it will unwind the stack, at some point releasing all your pointers in the vector; one giant memory leak.

If you use a more dedicated container, you will avoid this problem. Either write one yourself (not recommended, of course), or use boost pointer containers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文