我在使用这段特定代码时遇到了麻烦:虚拟函数似乎无法正常工作.
#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;
}
Run Code Online (Sandbox Code Playgroud)
我希望看到:
From CRectangle: Rectangle
From CTriangle: Triangle
Run Code Online (Sandbox Code Playgroud)
相反,我得到:
From CPolygon: Rectangle
From CPolygon: Triangle
Run Code Online (Sandbox Code Playgroud)
这是预期的行为吗?我该如何调用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;
}
Run Code Online (Sandbox Code Playgroud)
这是一个实例.
| 归档时间: |
|
| 查看次数: |
1045 次 |
| 最近记录: |