troubleshooting Question

Wrapper class for std::list that supports pointers and non-pointers

Avatar of Orcbighter
OrcbighterFlag for Australia asked on
29 Comments1 Solution583 ViewsLast Modified:
I have written a wrapper class around the std::list template.
I have not inherited from the std::list for many reasons; one of them being it has no virtual destructor.
My reasons for doing this are valid, and I will not go into them here.
However, I have hit a problem:
My class is defined as

template<class TYPE>
class List
    virtual ~List(void){};
    inline bool IsEmpty(void) { return m_list.empty(); }

    inline void Clear();
    std::list<TYPE> m_list;

in the inline file for the template declaration:

template<class TYPE>
void List<TYPE>::Clear(void)
    if ( !m_list.empty() )
        ListIndex iter = m_list.begin();
        TYPE item = *iter;

        if ( IsPointerType( item ))
            while ( m_list.size() > 0 )
                TYPE& item = m_list.front();
                delete item;                                 // <<-- problem is here

Now, if the list is populated with pointers to objects, this works well.
However, if the list template is used to store non-pointer objects, the code will not compile.
How can I come up with a definition that will support both objects and pointers to objects and still compile?
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 29 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 29 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros