Re: object used to convert a pointer to a c string
Finally I did this but I find it ugly because I have to explicitly write
code for each type I am assiging...
class IConvPtr
{
public:
IConvPtr(void* pVoidVal):m_ptr(pVoidVal)
{
;
}
IConvPtr& IConvPtr::operator=(void* pVoidVal) {m_ptr = pVoidVal;
return *this;}
IConvPtr& IConvPtr::operator=(CEBLOB* pBlobVal) {m_ptr = pBlobVal;
return *this;}
IConvPtr& IConvPtr::operator=(USHORT* pusVal) {m_ptr = pusVal;
return *this;}
IConvPtr& IConvPtr::operator=(wchar_t* pWStringVal)
{
// Do something specific
return *this;
}
operator void *()
{
return m_ptr;
}
private:
void* m_ptr;
};
Now I am trying with a templated option :
template <typename T>
class IConvPtr
{
public:
IConvPtr(T pVal) : m_ptr(pVal)
{}
IConvPtr& IConvPtr::operator=(T aptr)
{
m_ptr = aptr;
}
operator void *()
{
return m_ptr;
}
private:
void* m_ptr;
};
template <>
class IConvPtr<wchar_t *>
{
public:
IConvPtr& IConvPtr::operator=(wchar_t* aptr)
{
// Specific stuff
m_ptr = aptr;
}
private:
void* m_ptr;
};
and I am testing like that :
switch( gynPropVal->ulPropType )
{
case CEVT_LPWSTR: { pRet = IConvPtr<wchar_t *> (lpProp->val.lpwstr)
;break; }
case CEVT_UI4: { pRet = IConvPtr<USHORT *> (&(lpProp->val.uiVal))
;break; }
default: { pRet = (void*)0 ;break; }
}
but I get the following error :
error C2440: '<function-style-cast>' : cannot convert from 'wchar_t*
*' to 'IConvPtr<wchar_t*>'
1> No constructor could take the source type, or constructor
overload resolution was ambiguous
and this is normal because it's not a wchar_t* * but a wchar_t* and if I
change the template to with a pointer syntax it doesn't help.