c++ - Template class to close HANDLES and other WINAPI's? -
i'm new c++ , need help.
i want make template class
/struct
handles handle
, other winapi
s far code:
template <typename type_to_open, typename returntype, returntype (winapi * globalfn)( type_to_open )> class handle_wrap { public: type_to_open data; handle_wrap (type_to_open in_data) { data = in_data; } ~handle_wrap() { returntype (winapi * globalfn)( type_to_open );} };
handle_wrap <handle, bool, ::findclose> hfind ( findfirstfilea (pattern.c_str(), &ffd) );
i don't think working , compiler gives me warning:
warning c4101: 'globalfn' : unreferenced local variable
i saw code web , did changes it, , don't know if right way it?
the problem in destructor. repeat declaration of globalfn
, rather call it. should be:
~handlewrap() { (*globalfn)( data ); }
also, want make class copyable, movable or neither? if neither, should take steps prevent of relevant compiler generated defaults; otherwise, you'll need provide corresponding constructors (and possibly assignment operators). if copyable, you'll need sort of counter, shared between of copies, last destructor frees handle. movable (probably best solution, if can sure of having c++11), you'll need move constructor, ensure destructor of moved object no-op.
Comments
Post a Comment