C
Christian Christmann
Hi,
I need an STL list and was thinking of putting
the list in wrapper class.
The reason for my decision is that you can
much better perform consistency checks.
For instance, I need a function to append a
list to another. Using a wrapper class I could
do something like:
void List<ELEMENT>::Append(List<ELEMENT>* L)
{
if (!L) return;
mylist->splice(mylist->end(), *(L->GetList()));
}
Using the function splice directly without a wrapper
class wouldn't perform a check on !L and could
possibly transform the old list (mylist) in an inconsistent
state.
Another benefit of using a wrapper class is that you can
encapsulate functions which need more that one
statement. As example consider a function to return the next
element in a list:
ELEMENT* List<ELEMENT>::NextElement(ELEMENT* x)
{
if (mylist->empty()) return NULL;
typename list<ELEMENT*>::iterator it = find(mylist->begin(),
mylist->end(), x);
if (it != mylist->end())
return *(++(it));
else
return NULL;
}
No functions but just a direct use of the statements in the source code
would on the one hand produce unnecessarily more code and on the
other hand make the code worse readable.
Do you agree to my considerations or are you of the opinion that
a wrapper class for STL lists is an overkill.
Thank you for your comments.
Chris
I need an STL list and was thinking of putting
the list in wrapper class.
The reason for my decision is that you can
much better perform consistency checks.
For instance, I need a function to append a
list to another. Using a wrapper class I could
do something like:
void List<ELEMENT>::Append(List<ELEMENT>* L)
{
if (!L) return;
mylist->splice(mylist->end(), *(L->GetList()));
}
Using the function splice directly without a wrapper
class wouldn't perform a check on !L and could
possibly transform the old list (mylist) in an inconsistent
state.
Another benefit of using a wrapper class is that you can
encapsulate functions which need more that one
statement. As example consider a function to return the next
element in a list:
ELEMENT* List<ELEMENT>::NextElement(ELEMENT* x)
{
if (mylist->empty()) return NULL;
typename list<ELEMENT*>::iterator it = find(mylist->begin(),
mylist->end(), x);
if (it != mylist->end())
return *(++(it));
else
return NULL;
}
No functions but just a direct use of the statements in the source code
would on the one hand produce unnecessarily more code and on the
other hand make the code worse readable.
Do you agree to my considerations or are you of the opinion that
a wrapper class for STL lists is an overkill.
Thank you for your comments.
Chris