By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,098 Members | 1,881 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,098 IT Pros & Developers. It's quick & easy.

Pointer to a generic type parameter

P: n/a
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

Is there a posible way to get a this unmanaged pointer? My problem is
that it isn't allowed to creat a pin_ptr<DataTypeas a class member
and it takes quite a while to do it every function call. Or, do I I
realy have to do a hack and creat a void* from the array by using
GCHandle::Alloc(...).AddrOfPinnedObject().ToPointe r() and then cast it
to a byte* and shift it each time by sizeof(DataType)?

By the way, my generic typs are never reference objects, only value
structs and data types like int, byte, ... .

Thanks for any help!

Thorsten

generic<typename DataTypepublic ref class RingBuffer
{
array<DataType^m_Buffer;
DataType *m_Buffer_Ptr; // Error
}

Jan 24 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a

"interX" <TK***@gmx.dewrote in message
news:11**********************@s48g2000cws.googlegr oups.com...
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<DataTypeas 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 using
GCHandle::Alloc(...).AddrOfPinnedObject().ToPointe r() and then cast it
to a byte* and shift it each time by sizeof(DataType)?
sizeof (DataType) isn't known for a generic parameter, so neither you nor
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 DataTypepublic 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.
}

Jan 24 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.