[英]Virtual functions and vector iterator
我在使用这段特定代码时遇到了麻烦:虚拟函数似乎无法正常工作。
#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()函数来获得我期望的输出?
这是预期的行为吗? 我该如何调用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;
}
这是一个实例 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.