Re: Pointer to a generic type parameter
- From: "Ben Voigt" <rbv@xxxxxxxxxxxxx>
- Date: Wed, 24 Jan 2007 08:51:50 -0600
"interX" <TK777@xxxxxx> wrote in message
news:1169606820.120930.142350@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Hi
I'm new in VC++ and have a question to generics. I have a generic
class, which contains an array of the generic type. This array I can
pin and then I would like to get an unmanaged pointer to it. Therefore
I wanted to creat a class member which represents the pointer to the
array. Unfortunately I get the folowring compile error for the code
beneath:
error C3229: 'DataType *' : indirections on a generic type parameter
are not allowed
Generics are required to support all possible data types with the same
MSIL.... this isn't possible for what you are trying to do. Probably you
want a template instead.
Is there a posible way to get a this unmanaged pointer? My problem is
that it isn't allowed to creat a pin_ptr<DataType> as a class member
and it takes quite a while to do it every function call. Or, do I I
Probably you need to find a way to do what you want without pinning... so
much pinning is bad for performance, and trying to pin long-term is even
worse.
realy have to do a hack and creat a void* from the array by usingsizeof (DataType) isn't known for a generic parameter, so neither you nor
GCHandle::Alloc(...).AddrOfPinnedObject().ToPointer() and then cast it
to a byte* and shift it each time by sizeof(DataType)?
the C++ compiler can do pointer arithmetic. Only managed arrays contain the
extra information necessary to reference individual elements.
By the way, my generic typs are never reference objects, only value
structs and data types like int, byte, ... .
But an array is a managed type in .NET, and the garbage collector will move
it around.
Furthermore, even without using pointers, you'll find that generics are
totally useless with value types for doing anything more than containment,
because they don't implement much in the way of interfaces, any sort of math
is totally out of the question.
Thanks for any help!
Thorsten
generic<typename DataType> public ref class RingBuffer
{
array<DataType> ^m_Buffer;
DataType *m_Buffer_Ptr; // Error
interior_ptr<DataType*> might work... but still won't be pinned, and still
couldn't allow pointer arithmetic, because sizeof () isn't known, so I think
the compiler will still forbid it.
}
.
- References:
- Pointer to a generic type parameter
- From: interX
- Pointer to a generic type parameter
- Prev by Date: Re: System::String to wstring
- Next by Date: Re: Services, C#
- Previous by thread: Pointer to a generic type parameter
- Next by thread: How to serialize to binary data for TCP/IP communication
- Index(es):
Relevant Pages
|