Pointer to a generic Class

G

Guest

Hello

I'd like to write a Generic class that use a pointer to the generic structure.

I can't do this
unsafe public class PClass<S> where S : new()
{
public S* pData;

public PClass( )
{
pData = (S*) .....
}
}

Nor

public class PClass<S> where S : new()
{

unsafe public void Pippo( )
{
S myS = new S();
fixed (S* pSClass = &myS )
{
..............
}
}
}


While I can do this having defined MyStruct

unsafe public class PClass()
{
public void* pData;
public MyStruct* pData2;

public PClass( )
{
pData= (void*) .....
pData2= (MyStruct*) .....
}

unsafe public void Pippo( )
{
float[] myS = new float[100];
fixed (float* pSClass = &myS )
{
..............
}
}

}



HOW COME THE GENERIC VERSION IS NOT ALLOWED ?
I REALLY CAN'T FIGURE IT OUT.

Many thanks
 
D

Daniel O'Connell [C# MVP]

Dodo Cibelli said:
Hello

I'd like to write a Generic class that use a pointer to the generic
structure.

I can't do this
unsafe public class PClass<S> where S : new()
{
public S* pData;

public PClass( )
{
pData = (S*) .....
}
}

I do not believe this is possible.

As to why? Well, pointer work requires knowledge of the size of the
datatype, so I'd guess no one felt it was something that anyone needed. But
I don't know for sure.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top