简体   繁体   English

虚函数和向量迭代器

[英]Virtual functions and vector iterator

I'm having trouble with this specific piece of code: It seems like virtual functions don't work as I expected. 我在使用这段特定代码时遇到了麻烦:虚拟函数似乎无法正常工作。

#include <cstdio>
#include <string>
#include <vector>

class CPolygon
{
protected:
    std::string name;
public:
    CPolygon()
    {
        this->name = "Polygon";
    }
    virtual void Print()
    {
        printf("From CPolygon: %s\n", this->name.c_str());
    }
};

class CRectangle: public CPolygon
{
public:
    CRectangle()
    {
        this->name = "Rectangle";
    }
    virtual void Print()
    {
        printf("From CRectangle: %s\n", this->name.c_str());
    }
};

class CTriangle: public CPolygon
{
public:
    CTriangle()
    {
        this->name = "Triangle";
    }
    virtual void Print()
    {
        printf("From CTriangle: %s\n", this->name.c_str());
    }
};

int main()
{
    CRectangle rect;
    CTriangle trgl;
    std::vector< CPolygon > polygons;
    polygons.push_back( rect );
    polygons.push_back( trgl );

    for (std::vector<CPolygon>::iterator it = polygons.begin() ; it != polygons.end(); ++it)
    {
        it->Print();
    }

    return 0;
}

I expected to see: 我希望看到:

From CRectangle: Rectangle
From CTriangle: Triangle

instead I get: 相反,我得到:

From CPolygon: Rectangle
From CPolygon: Triangle

Is this expected behavior? 这是预期的行为吗? How should I call Print() function to get output expected by me? 我该如何调用Print()函数来获得我期望的输出?

Is this expected behavior? 这是预期的行为吗? How should I call Print() function to get output expected by me? 我该如何调用Print()函数来获得我期望的输出?

Yes, it is expected behavior. 是的,这是预期的行为。

The problem is that standard containers, including vector , have value semantics : they store copies of the objects you pass to push_back() . 问题是标准容器(包括vector )具有值语义 :它们存储传递给push_back()的对象的副本 Polymorphism, on the other hand, is based on reference semantics - it requires references or pointers in order to work correctly. 另一方面,多态性基于引用语义 - 它需要引用或指针才能正常工作。

What happens in your case is that your CPolygon object gets sliced , which is not what you want. 在你的情况下会发生的是你的CPolygon对象被切片 ,这不是你想要的。 You should store pointers (possibly smart pointers) in your vector rather than objects of type CPolygon . 您应该在向量中存储指针 (可能是智能指针)而不是CPolygon类型的对象。

This is how you should rewrite your main() function: 这是你应该重写main()函数的方法:

#include <memory> // For std::shared_ptr

int main()
{
    std::vector< std::shared_ptr<CPolygon> > polygons;
    polygons.push_back( std::make_shared<CRectangle>() );
    polygons.push_back( std::make_shared<CTriangle>() );

    for (auto it = polygons.begin() ; it != polygons.end(); ++it)
    {
        (*it)->Print();
    }

    return 0;
}

Here is a live example . 这是一个实例

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM