【发布时间】:2012-03-01 03:50:28
【问题描述】:
我有一个基类line,它有一个子类arc(请注意,其他类将从line继承)...我需要存储一个列表,或者vector的line对象可能是line 或arc。 arc 具有line 没有的附加属性。因此,当我处理我的列表或line 对象中的vector 时,如何确定该对象是line 还是arc?
我准备了一个小示例程序,并在 cmets 中放入了我将尝试完成的示例的伪代码。这甚至可能吗?
#include <iostream>
#include <vector>
#include <iterator>
using namespace std;
struct point
{
double x;
double y;
};
class line
{
public:
point start;
point end;
};
class arc: public line
{
public:
point center;
};
int main( int argc, char* argv[] )
{
vector<line*> a;
a.push_back( new line );
a.push_back( new arc );
for( vector<line*>::iterator i = a.begin(); i < a.end(); i++ )
{
(*i)->start.x = 10;
(*i)->start.y = 11;
(*i)->end.x = 101;
(*i)->end.y = 102;
//if( type of (*i) is arc )
//{
// (i)->center.x = 111;
// (i)->center.y = 112;
// }
}
return 0;
}
【问题讨论】: