2
循環std :: list時如何檢查對象類型?迭代std :: list <boost :: variant>
class A
{
int x; int y;
public:
A() {x = 1; y = 2;}
};
class B
{
double x; double y;
public:
B() {x = 1; y = 2;}
};
class C
{
float x; float y;
public:
C() {x = 1; y = 2;}
};
int main()
{
A a; B b; C c;
list <boost::variant<A, B, C>> l;
l.push_back(a);
l.push_back(b);
l.push_back(c);
list <boost::variant<A, B, C>>::iterator iter;
for (iter = l.begin(); iter != l.end(); iter++)
{
//check for the object type, output data to stream
}
}
std :: list?呸! – 2010-07-22 17:10:57
因爲它是私有的,所以不能輸出數據。如果有方法,那麼推薦的方法是使用'boost :: static_visitor'。 – UncleBens 2010-07-22 17:38:05
@ Billy ONeal爲什麼不呢?我選擇它是因爲我將從列表中的任何地方移除對象。 – cpx 2010-07-22 17:54:29