MC++ passing pointer to 'int'

E

Edward Diener

I need to pass a pointer to 'int' as a parameter in a managed module to a
managed member function. I am assuming I need to box my value. Is this true,
or can I just pass the address of my variable ? The variable needs to
conform to CLS specifications.
 
R

Ronald Laeremans [MSFT]

Hi Edward,

Ref parameters are part of CLS. Just pass a pointer to a System::Int32 (or
equivalently, a __gc pointer to an int).

Ronald Laeremans
Visual C++ team
 
E

Edward Diener

Ronald said:
Hi Edward,

Ref parameters are part of CLS. Just pass a pointer to a
System::Int32 (or equivalently, a __gc pointer to an int).

I have some confusion about this. My understanding is that System::Int32 is
a value type. So this means that if I pass a pointer to a System::Int32
object, I can just instantiate one on the stack and take its address to pass
it as a pointer, since as the documentation states, there is an implicit
conversion from a V __nogc * to a V __gc * , ie.

__gc class A { void SomeMemberFunction(System::Int32 *); }
// System::Int32 * is a __gc pointer because System::32 is a managed type.
A * myA = new A();
System::Int32 avar;
myA -> SomeMemberFunction(&avar);

Now suppose I have an 'int' instead:

int avar;
myA -> SomeMemberFunction(&avar);

Does this work ? or do I have to do manual boxing such as:

myA -> SomeMemberFunction(__box(avar));
 

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