dude_1967
July 8th, 2002, 06:48 AM
Gurus,
I am uncertain of the proper use of run-time polymorphism and memory cleanup in STL-list.
Often one manages lists of pointers to objects in an STL-list. Imagine then a list of pointers to base, whereby base has two derived classes derived1 and derived2.
The approach shown in the code snippet below is my standard usage, and it seems to work well with VC and GNU. Is the use of run-time polymorphism correct in the code example below? Is it guaranteed in C++ that (*it)->f() must properly interpret the type of the pointed to object?
In addition, look at the memory cleanup. The right destructors will be called, OK. Is it also necessary to remove the objects from the list itself using "erase"?.
Thanks. Chris.
#include <list>
#include <iostream>
using namespace std;
class base
{
private:
int m1;
public:
base() { m1 = 1; }
virtual ~base() { cout << "delete c1" << endl; }
virtual void f(void) { cout << m1 << " " << "function1" << endl; }
};
class derived1 : public base
{
private:
int m2;
public:
derived1() { m2 = 2; }
virtual ~derived1() { cout << "delete c2" << endl; }
virtual void f(void) { cout << m2 << " " << "function2" << endl; }
};
class derived2 : public derived1
{
private:
int m3;
public:
derived2() { m3 = 3; }
virtual ~derived2() { cout << "delete c3" << endl; }
virtual void f(void) { cout << m3 << " " << "function3" << endl; }
};
int main(int argc, char* argv[])
{
list<base*> ptr_list;
base* p;
// Add a new base to list
p = new base;
ptr_list.push_back(p);
// Add a new derived1 to list
p = new derived1;
ptr_list.push_back(p);
// Add a new derived2 to list
p = new derived2;
ptr_list.push_back(p);
list<base*>::iterator it;
// Do some kind of operation on the list elements
for(it = ptr_list.begin(); it != ptr_list.end(); it++)
{
(*it)->f();
}
// Memory cleanup
for(it = ptr_list.begin(); it != ptr_list.end(); it++)
{
// Is this correct?
// Is it enough memory cleanup?
// Is it necessary to remove elements from the list?
delete *it;
}
return 1;
}
:)
I am uncertain of the proper use of run-time polymorphism and memory cleanup in STL-list.
Often one manages lists of pointers to objects in an STL-list. Imagine then a list of pointers to base, whereby base has two derived classes derived1 and derived2.
The approach shown in the code snippet below is my standard usage, and it seems to work well with VC and GNU. Is the use of run-time polymorphism correct in the code example below? Is it guaranteed in C++ that (*it)->f() must properly interpret the type of the pointed to object?
In addition, look at the memory cleanup. The right destructors will be called, OK. Is it also necessary to remove the objects from the list itself using "erase"?.
Thanks. Chris.
#include <list>
#include <iostream>
using namespace std;
class base
{
private:
int m1;
public:
base() { m1 = 1; }
virtual ~base() { cout << "delete c1" << endl; }
virtual void f(void) { cout << m1 << " " << "function1" << endl; }
};
class derived1 : public base
{
private:
int m2;
public:
derived1() { m2 = 2; }
virtual ~derived1() { cout << "delete c2" << endl; }
virtual void f(void) { cout << m2 << " " << "function2" << endl; }
};
class derived2 : public derived1
{
private:
int m3;
public:
derived2() { m3 = 3; }
virtual ~derived2() { cout << "delete c3" << endl; }
virtual void f(void) { cout << m3 << " " << "function3" << endl; }
};
int main(int argc, char* argv[])
{
list<base*> ptr_list;
base* p;
// Add a new base to list
p = new base;
ptr_list.push_back(p);
// Add a new derived1 to list
p = new derived1;
ptr_list.push_back(p);
// Add a new derived2 to list
p = new derived2;
ptr_list.push_back(p);
list<base*>::iterator it;
// Do some kind of operation on the list elements
for(it = ptr_list.begin(); it != ptr_list.end(); it++)
{
(*it)->f();
}
// Memory cleanup
for(it = ptr_list.begin(); it != ptr_list.end(); it++)
{
// Is this correct?
// Is it enough memory cleanup?
// Is it necessary to remove elements from the list?
delete *it;
}
return 1;
}
:)