How to build boost foreach cycle

Posted by Ockonal on Stack Overflow See other posts from Stack Overflow or by Ockonal
Published on 2010-05-26T17:36:32Z Indexed on 2010/05/26 17:41 UTC
Read the original article Hit count: 302

Filed under:
|
|
|
|

Hi guys! I have some abstract class called IClass (has pure virtual function). There are some classes which inherit IClass: CFirst, CSecond. I want to add objects of classes which inherit into boost::ptr_vector:

class IClass { virtual void someFunc() = 0; };
class CFirst : public IClass { };
class CSecond : public IClass { };

boost::ptr_vector<IClass> objectsList;

objectsList.push_back(new CFirst());
objectsList.push_back(new CSecond());

And now my goal is to call function (which is declarated in IClass) from all objects in objectsList. I'd prefer to use BOOST_FOREACH:

foreach(IClass tempObj, objectsList)
{
    tempObj.someFunc();
}

The problem is that IClass - abstract class, so I can't make instance of it for the foreach-cycle. What should I do?

© Stack Overflow or respective owner

Related posts about boost

Related posts about function