Thanks for the thoughts. I ended up doing this with a auto_ptr<
shared_ptr<T> >.
Regards,
Carter.
On May 12, 5:56 am, Gianni Mariani <gi4nos...@[EMAIL PROTECTED]
> wrote:
> Carter wrote:
> > Hi,
>
> > I am currently working on a project in C++ but I am somewhat unsure
> > about how to implement the equivalent of C/Java style handles. I am
> > curious about the correct idiomatic way to do this. My problem is as
> > follows I have a set of elements which I want to update to a new value
> > all at the same time. I figure I would use a handle for this.
>
> > In C this might be a T** handle. and possibly an array/linked list
> > holding the pointers T* array[N]. What is the best way to do something
> > like this in C++?
>
> There are many C++ ways of doing this. The technique I like the most is
> the one that gives you the greatest type safety. A handle can usually
> refer to different types of "things" being handled so you try to build a
> class heirarchy.
>
> class HandleBase
> {
> protected:
> HandleType m_handle;
>
> public:
> HandleBase(HandleType i_handle)
> : m_handle( i_handle )
> {
> }
>
> virtual void Close()
> {
> ... close handle ...
> }
>
> virtual ~HandleBase()
> {
> Close();
> }
>
> // no copying ...
> private:
> HanldeBase(const HanldeBase &);
> HanldeBase operator=(const HanldeBase &);
>
> };
>
> Now, the question is, how do you manage HandleBase which is a simple
> matter of defining how you want them to behave. I like managing them
> with shared pointers which means when all references to a handle go
> away, the handle is magically closed.
>
> e.g.
>
> std::vector<shared_ptr<HanldeBase> > handles;
>
> handles.push_back( new HandleBase(handle) );


|