"JustSomeGuy" <no**@nottelling.com> wrote in message
news:KaHKc.54440$Mr4.16678@pd7tw1no...
using namespace std;
class a : public list<baseclass>
{
int keya;
};
class b : public list<a>
{
int keyb;
};
class c : public list<b>
{
int keyc;
};
void c::cleanItUp(c & x)
{
cout << "The size of c class list is " << x.size() << endl;
for ( c::iterator cit = x.begin(); cit != x.end(); ++cit )
{
cout << "The size of b class list is " << cit->size() << endl;
for ( b::iterator bit = cit->begin(); bit != cit->end(); ++bit )
{
cout << "The size of a class list is " << bit->size() << endl;
for ( a::iterator ait = bit->begin(); ait != bit->end();
++ait ) {
cout << "The size of baseclass class list is " <<
ait->size() << endl;
}
}
}
}
At some point one instance of the baseclass is added to the list...
However for this to occure then one instance of a and one instance of b
must also exist.
This is done by the c::add(baseclass & x) method.
My question is... if that instance is removed then the parents need to be
removed as well.
So how do you check for this? ie a parent without children should remove
itself?
The code you have provided is incomplete, but I'll tell you what I can.
Given the current implementation of the c class, it is impossible for it to
know when an instance of b, a, or baseclass has been added or removed from
its ancestor. Some other code could call std::list::push_back(),
std::list::erase(), std::list::clear(), etc, and the c class would have its
members modified without its knowledge or consent. The standard containers
do not provide any way for derived clases to be notified of modifications.
If you wish to control access to the std::list members of c, b, and a, then
you may want to consider making those std::lists private members of the
appropriate class and only exposing a very limited interface to the users of
the c class. That way, it may be easier to check for parents without
children, as well as guarantee other conditions that cannot be guaranteed by
the current implementation. In that scenario, it would be easy for a member
function c::add(baseclass & x), could have a member function
c::remove(<something>) to "undo" that operation. That is just a guess. I'd
have to know more about what you are doing to give you a complete answer.
--
David Hilsee