template <class T, class FPTR = void (void *)>
class CSmartPtr
{
public :
// -----------------------------------------------------------------------
// Constructors and Destructor
// -----------------------------------------------------------------------
CSmartPtr()
{
m_TData = NULL;
m_deleter = NULL;
}
CSmartPtr(T* TData)
{
m_TData = TData;
m_deleter = NULL;
}
CSmartPtr(T* TData, FPTR *deleter)
{
m_TData = TData;
m_deleter = deleter;
}
~CSmartPtr()
{
if (NULL == m_deleter)
{
delete m_TData;
}
else
{
m_deleter(m_TData);
}
}
T *const operator-> () const
{
return m_TData;
}
//-----------------------------------------------------------------------
// Getter methods
//-----------------------------------------------------------------------
T *const get() const
{
return m_TData;
}
void reset(T* TData)
{
if (NULL != m_TData)
{
if (NULL == m_deleter)
{
delete m_TData;
}
else
{
m_deleter(m_TData);
}
}
m_TData = TData;
}
private :
// -----------------------------------------------------------------------
// Private data members
//
// fUnicodeForm
// This is the Unicode XMLCh format of the string.
// -----------------------------------------------------------------------
T *m_TData;
FPTR *m_deleter;
};
int main( void )
{
CSmartPtr<BYTE> apbySrcBuff; // This doesnt work.
CSmartPtr<BYTE, (void) (void *)> apbySrcBuff; // This works.
getchar();
return TRUE;
}
我绝对是templates
新手,我被上述问题困住了,从几个小时开始。试图自己解决,但没有成功。
错误Error 1 error C2064: term does not evaluate to a function taking 1 arguments
该行是 m_deleter(m_TData);
代码顶部需要以下内容:
#include <cstdio>
typedef char BYTE;
bool TRUE = true;
并在注释掉您的一个相同的内容后:
CSmartPtr apbySrcBuff;
行,都编译了。
我已经在 ideone 上测试了它并且运行良好(它以运行时错误终止,但这是您的smart_ptr实现,我没有调试(。
我发现的唯一问题是第二个声明使用了额外的参数,(void)(void*)
并且编译在这里失败(我对第一个声明没有问题(。