0

I have a class like this

    template <typename T, typename Policy>
    AutoObjectRelease {
public:
    AutoObjectRelease(T& obj):obj_(obj){}

    ~AutoObjectRelease(){ Policy::Release(obj_); }
private:
    T&  obj_;
}

I wonder how one would you name this class, as it seems to me this could be quite a common pattern. I also wonder in which other situations you would use such a class. In my case I use the singleton object only sporadically and I wanted to make sure it doesn't remain loaded for the duration of the application. What are your thoughts on this ? Another idea would be to make the singleton object reference counted, but don't know exactly what would be the benefits for a singleton object - it might only complicate the implementation (need to make it thread safe also)

1 Answer 1

3

It's called a smart pointer. Specifically, the unique version is in the next Standard as a unique_ptr, and the reference-counted version is in Boost, TR1 and C++11 as shared_ptr.

As for it's uses with Singletons, let me give you a hint: Don't use Singletons and you won't have many, many, many problems.

This is exactly a smart pointer, but with default deletion not provided.

5
  • Saying that you shouldn't use it is no good if you don't provide the alternative implementation. Here's one: Put a local variable in main().
    – tp1
    Sep 5, 2011 at 17:49
  • @DeadMG it is kind of a smart pointer only that in the destructor it does not call delete on the "owned" object, and this can't be included also in the class of "custom deleters" of the object. Actually in my case I don't own the wrapped object.
    – Ghita
    Sep 5, 2011 at 17:52
  • @tp1 if I put a local variable in main then I will have this object life for the duration of the application :-)
    – Ghita
    Sep 5, 2011 at 17:53
  • @tp1: There are as many alternatives to singletons as there are uses for them.
    – greyfade
    Sep 5, 2011 at 18:04
  • @tp1: You would only need a Singleton alternative if you actually needed a Singleton in the first place. That scenario, for all intents and purposes, does not exist.
    – DeadMG
    Sep 5, 2011 at 18:23

Not the answer you're looking for? Browse other questions tagged or ask your own question.