Native types and Generics
I want to create a native array and access it managed code. I don't want to re write code to different types, (int
, long
, float
, double
) therefore tried using generics.
typedef int IND;
generic <typename T>
public ref class ntvarray
{
void *pnt;
IND sz;
public:
ntvarray(IND length)
{
sz = sizeof(T);
pnt = ::operator new(length*sz);
}
~ntvarray()
{
::operator delete(pnt);
}
void* pointer()
{
return pnt;
}
T getitem (IND index)
{
//c3229
return ((T*)pnt开发者_运维知识库)[index];
}
void setitem (IND index, T value)
{
//c3229
((T*)pnt)[index] = value;
}
};
I am getting the error and I know the reason for this error,
error C3229:
'T *'
: indirections on a generic type parameter are not allowed
However is there a way to do this using generics? Any other way to do this, may be something else other than using generics?
No, you can't do this using generics. But you can use templates. This avoids the code duplication, which your question emphasizes, but won't allow run-time instantiation like generics would.
精彩评论