Managed pointers to pointers

  • Thread starter Thread starter Mark Ingram
  • Start date Start date
M

Mark Ingram

Hi, I've been trying to update some old C++ code and when i came across
this:

MyClass** ppOut

I changed it to:

MyManagedClass^% ppOut


However - it doesn't work exactly the same because I am unable to pass
nullptr for one of those parameters. How can I replicate pointers to
pointers?

Thanks,
 
Mark Ingram a écrit :
Hi, I've been trying to update some old C++ code and when i came across
this:

MyClass** ppOut

I changed it to:

MyManagedClass^% ppOut

"MyManagedClass^%" (tracking reference to a managed handle) is the
managed equivalent of "MyClass*&" (reference to a pointer). It is NOT
the equivalent of a pointer to a pointer.
However - it doesn't work exactly the same because I am unable to pass
nullptr for one of those parameters. How can I replicate pointers to
pointers?

You cannot do it directly, but on the other hand is shouldn't be
necessary in most cases. In native C and C++, pointers to pointers (to
pointers....) are mainly usefull for multi-dimensionnal arrays - which
are handled differently in managed code.

On the other hand, if you were using a pointer to a pointer in native
code as a function parameter, you should use the [Out] attribute
instead.

Show us the original code so that we can best advise you on how to
replace it.

Arnaud
MVP - VC
 
Show us the original code so that we can best advise you on how to
replace it.

Arnaud
MVP - VC

After a bit of hunting I've found interior_ptr<MyManagedClass> which
works exactly as I was hoping.

Basically:

MyManagedClass^ pTest;
MyFunction(&pTest);

void MyFunction(interior_ptr<MyManagedClass> ppTest)
{
if (ppTest != nullptr)
{
*ppTest = gcnew MyManagedClass();
}
}

Is that ok?
 
Mark Ingram said:
After a bit of hunting I've found interior_ptr<MyManagedClass> which works
exactly as I was hoping.

Basically:

MyManagedClass^ pTest;
MyFunction(&pTest);

void MyFunction(interior_ptr<MyManagedClass> ppTest)
 
Back
Top