How do we ususaly deal with a vector whose elements are pointers to object? My specific question is the comment at the end of the code supplied below. Thanks.
class A
{
public:
virtual int play() = 0 ;
};
class B : public A
{
public:
int play() {cout << "play in B " << endl;};
};
class C : public A
{
public:
int play() {cout << "play in C " << endl;};
};
int main()
{
vector<A *> l;
l.push_back(new B());
l.push_back(new C());
for(int i = 0 ; i < l.size();i++)
{
l[i]->play();
}
//Do i have to do this to avoid memory leak? It is akward. Any better way to do this?
for(int i = 0 ; i < l.size();i++)
{
delete l[i];
}
}
See Question&Answers more detail:os