Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ > Re: handles in ...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 2 of 3 Topic 45866 of 47925
Post > Topic >>

Re: handles in C++

by Gianni Mariani <gi4nospam@[EMAIL PROTECTED] > May 11, 2008 at 09:56 PM

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) );
 




 3 Posts in Topic:
handles in C++
Carter <cartercheng@[E  2008-05-11 11:25:06 
Re: handles in C++
Gianni Mariani <gi4nos  2008-05-11 21:56:10 
Re: handles in C++
Carter <cartercheng@[E  2008-05-12 18:54:39 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Mon Oct 6 17:51:59 CDT 2008.