template<class T, class CloseT, CloseT (_stdcall * CloseFunc)(T)> class THandle {
T m_h;
public:
THandle() {
m_h = T(-1); // Always illegal
}
THandle(T h) { m_h = h; }
~THandle() {
if (IsValid()) Close(); // Ensure we do not double-close
}
THandle<T, CloseT, CloseFunc>& operator=(T h) {
m_h = h;
return *this;
}
operator T() { return m_h; }
T *operator &() { return &m_h; }
CloseT Close() {
T h = m_h;
m_h = T(-1);
return (*CloseFunc)(h); // Return the result of whatever type
};
BOOL IsValid() { return m_h != T(-1) && m_h != T(0); }
};
//
// Now define common handles as safe classes.
//
typedef class THandle<HANDLE, BOOL, &CloseHandle> CHandle;
typedef class THandle<HKEY, LONG, &RegCloseKey> CHKey;
typedef class THandle<HANDLE, BOOL, &CloseHandle> CHFile;
typedef class THandle<HANDLE, BOOL, &FindClose> CHFind;
typedef class THandle<HANDLE, BOOL, &FindCloseChangeNotification> CHChange;
typedef class THandle<HMODULE, BOOL, &FreeLibrary> CHModule;
typedef class THandle<HWND, BOOL, &DestroyWindow> CHWnd;
//代码见上,VC6编译的时候报
error C2440: 'specialization' : cannot convert from 'int (__stdcall *)(void *)' to ' (__stdcall *)()'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
error C2975: 'THandle' : invalid template argument for 'CloseFunc', constant expression expected see declaration of 'THandle'
.......
WHY?
T m_h;
public:
THandle() {
m_h = T(-1); // Always illegal
}
THandle(T h) { m_h = h; }
~THandle() {
if (IsValid()) Close(); // Ensure we do not double-close
}
THandle<T, CloseT, CloseFunc>& operator=(T h) {
m_h = h;
return *this;
}
operator T() { return m_h; }
T *operator &() { return &m_h; }
CloseT Close() {
T h = m_h;
m_h = T(-1);
return (*CloseFunc)(h); // Return the result of whatever type
};
BOOL IsValid() { return m_h != T(-1) && m_h != T(0); }
};
//
// Now define common handles as safe classes.
//
typedef class THandle<HANDLE, BOOL, &CloseHandle> CHandle;
typedef class THandle<HKEY, LONG, &RegCloseKey> CHKey;
typedef class THandle<HANDLE, BOOL, &CloseHandle> CHFile;
typedef class THandle<HANDLE, BOOL, &FindClose> CHFind;
typedef class THandle<HANDLE, BOOL, &FindCloseChangeNotification> CHChange;
typedef class THandle<HMODULE, BOOL, &FreeLibrary> CHModule;
typedef class THandle<HWND, BOOL, &DestroyWindow> CHWnd;
//代码见上,VC6编译的时候报
error C2440: 'specialization' : cannot convert from 'int (__stdcall *)(void *)' to ' (__stdcall *)()'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
error C2975: 'THandle' : invalid template argument for 'CloseFunc', constant expression expected see declaration of 'THandle'
.......
WHY?
解决方案 »
- 求图像分割源码
- 求教,控件问题!
- 使用updatedata(true)时控件传变量失败(cstring->char)
- 在线等待,高手帮忙解决一下!l list control问题!我在线等!
- 怎样取得应用程序当前目录?
- snmp++,get-next 时连续发出两条一样的消息是什么原因?
- 一个折磨我好长时间的问题:关于Microsoft ADO Data Control的使用,通常使用这个控件是在属性设置中处理的,现在我想在程序中解决,我该
- 一个简单的问题?
- 基于FFT的图像匹配
- 请问诸位大侠如何在VC中得到Win98/NT/2000的网上邻居(NetWork)中的本组组名???
- 我这样做的聊天小程序有固守IP的服务器上运行 在内网的客户端能放问吗
- VC6以后是什么?
#include <windows.h>
#pragma comment (lib,"advapi32")
#pragma comment (lib,"user32")
template<class T, class CloseT, CloseT (_stdcall * CloseFunc)(T)> class THandle {
T m_h;
public:
THandle() {
m_h = T(-1); // Always illegal
}
THandle(T h) { m_h = h; }
~THandle() {
if (IsValid()) Close(); // Ensure we do not double-close
}
THandle<T, CloseT, CloseFunc>& operator=(T h) {
m_h = h;
return *this;
}
operator T() { return m_h; }
T *operator &() { return &m_h; }
CloseT Close() {
T h = m_h;
m_h = T(-1);
return (*CloseFunc)(h); // Return the result of whatever type
};
BOOL IsValid() { return m_h != T(-1) && m_h != T(0); }
};
//
// Now define common handles as safe classes.
//
typedef class THandle<HANDLE, BOOL, &CloseHandle> CHandle;
typedef class THandle<HKEY, LONG, &RegCloseKey> CHKey;
typedef class THandle<HANDLE, BOOL, &CloseHandle> CHFile;
typedef class THandle<HANDLE, BOOL, &FindClose> CHFind;
typedef class THandle<HANDLE, BOOL, &FindCloseChangeNotification> CHChange;
typedef class THandle<HMODULE, BOOL, &FreeLibrary> CHModule;
typedef class THandle<HWND, BOOL, &DestroyWindow> CHWnd;
//void main()
{
CHandle hd;
hd.Close();
CHKey ke;
ke.Close();
CHFile fe;
fe.Close();
CHFind fd;
fd.Close();
CHChange ce;
ce.Close();
CHModule me;
me.Close();
CHWnd wd;
wd.Close();}
我的VC已安装了sp5
装个sp5试试(microsoft网站上已有sp6了)
这个错误在MSDN上被视为VS的一个BUG。但是是说安装SP3就可以搞定,我都安装SP5了。
谢谢keiy 。已给分。