views:

38

answers:

1

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?

+1  A: 

Use a reference to IClass instead:

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