虚函数和向量迭代器

4
我遇到了代码上的困难:
看起来虚函数并不按我预期那样工作。
#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;
}

我期望看到:

From CRectangle: Rectangle
From CTriangle: Triangle

我得到的是:
From CPolygon: Rectangle
From CPolygon: Triangle

这是预期行为吗?我应该如何调用 Print() 函数来获得我期望的输出?
1个回答

5

这是预期的行为吗?我该如何调用Print()函数以获得我期望的输出?

是的,这是预期的行为。

问题在于标准容器(包括vector)具有值语义:它们存储您传递给push_back()的对象的副本。另一方面,多态性基于引用语义 - 它需要引用或指针才能正常工作。

在您的情况下会发生的是,您的CPolygon对象被切片,这不是您想要的。您应该在向量中存储指针(可能是智能指针),而不是CPolygon类型的对象。

以下是您应该重写的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;
}

这里有一个实时示例


谢谢!它起作用了,我还尝试了使用标准指针的示例,也起作用了。 - TigerInALoop
什么是shared_ptr<>和auto?[初学者] - Dineshkumar
@Dineshkumar:这是一个广泛的主题,我建议你在 StackOverflow 上提出两个新问题 :) (但不要忘记先做一些研究,答案可能已经在那里了) - Andy Prowl

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接