tags:

views:

165

answers:

7
    template <class T>
    class container
    {
        typedef list<T> ObjectList;
public:
    ~container ()
    {
        for (typename ObjectList::iterator item = _Container.begin(); item != _Container.end(); item++)
        {
        if (*item) 
        delete (*item)
        }
    }
    }

how can i free the container items by deleting the pointer content? g++ not allow this code

+3  A: 

Let's see:

  1. _Container is never declared.

  2. delete(*item) needs a semi-colon.

  3. You don't need to test a pointer before deleteing it - you can delete 0.

  4. You've got to be sure T is a pointer type.

  5. Don't use raw pointers like this - it's better to use some good smart pointer.

Douglas Leeder
+1  A: 

You don't.

template<typename T>
class container {
    typedef list<T> list_t;
    list_t container_;
    // no user-defined destructor needed
};

The member "container_" manages its elements on its own.

If, however, you want to store pointers to dynamically allocated objects and let container take "ownership" (responsilibity to delete) you can iterate through the list and call delete on each element. But then, you also need to think about what should happen when this container object is copied or assigned to another one! The copies would share the same pointees and some elements might get deleted twice!

BTW: You should not use names that start with an underscore. Those names are reserved.

sellibitze
i think _container may be problem, but _Container does not. Not sure
jack london
It's the other way around: Anything that starts either with two underscores or just one underscore followed by an upper case letter is a reserved name you should not use.
sellibitze
Read this for naming constraints: (what-are-the-rules-about-using-an-underscore-in-a-c-identifier)[http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier/228797#228797]
Martin York
A: 

You don't want to do that -- the container (ObjectList, in your case) owns the items it contains, so to delete them, you need to tell it what you want, as in: ObjectList.erase(item);. Since you're (apparently) deleting all the items, you might as well use: ObjectList.clear(); and skip using your explicit loop at all.

Then again, since ObjectList is apparently an std::list, you don't need to do any of the above -- when you destroy an std::list, it automatically destroys whatever objects it contains.

Edit: (mostly in response to UncleBens' comment): If you're trying to create a container of pointers that manages deleting the items pointed to by the pointers it contains, then the first thing you want to do is ensure that it really contains pointers:

template <class T>
class container { 
    typedef typename std::list<T>::iterator it;
    std::list<T *> items;    // Note 'T *' rather than just "T"
public: 
    ~container() { 
        for (it p=items.begin; it!=items.end(); ++it)
             delete *it;     // *it yields a pointer from the container.      
    }    
};
Jerry Coffin
I assume OP has something like boost.ptr_containers in mind (even though it is hard to tell from the question). Having a container that does delete contained pointers is a perfectly kosher thing to have.
UncleBens
A: 
  • there are typo errors
  • you are trying to delete which is not a pointer

Hope This will work for you !!!

template <class T>
class container
{

public:
    typedef list<T*> ObjectList;
    ~container ()
    {
        for (ObjectList::iterator item = _Container.begin(); item != _Container.end(); item++)
        {
      if (*item) 
       delete (*item);
        }
    }
    ObjectList _Container;
};
sat
What about the implicitly generated copy c'tor and assignment operator?
sellibitze
Please read this: http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier/228797#228797 _Container is reserved for use by the OS. If the OS builders decides to add a macro with this name to the standard headers it will mess up your code.
Martin York
You don't need to check for null before deleting.
GMan
I just wanted to show why he is facing compilation error, I was not suppose to write whole design for him
sat
A: 

It is not immediately clear what you are trying to implement, but if you are trying to create a container that stores pointers to objects (instead of storing objects themselves), then a better technique would be to make a container of smart pointers (the specific kind of smart pointer depends on your intent). That would automatically take care of object lifetime management.

AndreyT
+3  A: 

Stop trying to do this yourself and use a Boost Pointer Container.

Mark Ransom
Must travel the path of rocks to get to paradise.
lsalamon
The path of rocks known as C++ has been travelled many times. Best to stick to the path and not wander from it.
Mark Ransom
I agree you should wonder off the path. Get beaten to near death by the bandits then compare what you did wrong to the boost. That way you learn something new. But before you amble off, understand this is a solved problem.
Martin York
+1  A: 

Or you could use the boost ptr containers that do all the hard work for you.

http://www.boost.org/doc/libs/1%5F40%5F0/libs/ptr%5Fcontainer/doc/ptr%5Flist.html

Martin York