Thread: delete functor

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    330

    delete functor

    One way to write a delete functor to delete all pointers in a container is:

    Code:
    template<typename T>
    struct Deleter : public std::unary_function<const T *, void>
    {
      void operator()(const T *Object) const
      {
        delete Object;
      }
    };
    Now Im wondering if the following is a correct safe delete functor, I'm talking specifically about the pointer reference. Also is the first unary_function template paramater correct? (const T *&)

    Code:
    template<typename T>
    struct SafeDeleter : public std::unary_function<const T *&, void>
    {
      void operator()(const T *&Object) const
      {
        delete Object;
        Object = NULL;
      }
    };

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    o_O

    Why? Why not just use a smart pointer? (You just can't use `std::auto_ptr<???>'.)

    A reference of a pointer to a constant value is not the same as a reference to a pointer to a mutable value.

    Do you really want two distinct deleter functions?
    Or one that behaves differently depending on the mutability of the reference?

    It seems like a tremendously bad idea.

    Soma

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I second smart pointers, but this seems OK too (if you don't have boost/compiler supporting C++0x features).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Proper Usage of the delete Operator
    By thetinman in forum C++ Programming
    Replies: 7
    Last Post: 04-25-2007, 11:53 PM
  2. BST delete again, but this time I think I'm close
    By tms43 in forum C++ Programming
    Replies: 9
    Last Post: 11-05-2006, 06:24 PM
  3. delete and delete []
    By Lionel in forum C++ Programming
    Replies: 8
    Last Post: 05-19-2005, 01:52 PM
  4. why is this prog crashing on delete?
    By Waldo2k2 in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 11:17 PM
  5. Problem need help
    By Srpurdy in forum C++ Programming
    Replies: 1
    Last Post: 07-24-2002, 12:45 PM