void* equivalent

N

Nanditha Chandra

Hi!

I am new to Managed C++ and am working on my first project with this.

In native C++, we could have a function that takes void* and call that
function with any type. Is there a way to do that with MC++? If I had
something as follows in native C++,

Foo(void* arBuf, int nSize);

And this can be called as follows

int* nBuf = new int[nSize];
Foo(nBuf, nSize);

long* nBuf2 = new long[nSize];
Foo(nBuf2, nSize);

How would I do something like this in MC++? I have an array of Byte
and int (and possibly others as well). And I need to pass this to a
function which can handle both (Similar to the Foo function above). I
am not sure how that Foo function has to be defined in MC++?

Also an additional question - What exactly is IntPtr? Is it void
equivalent?

Thanks,
D
 
T

Tomas Restrepo \(MVP\)

Nanditha,
I am new to Managed C++ and am working on my first project with this.

In native C++, we could have a function that takes void* and call that
function with any type. Is there a way to do that with MC++? If I had
something as follows in native C++,

Foo(void* arBuf, int nSize);

And this can be called as follows

int* nBuf = new int[nSize];
Foo(nBuf, nSize);

long* nBuf2 = new long[nSize];
Foo(nBuf2, nSize);

How would I do something like this in MC++? I have an array of Byte
and int (and possibly others as well). And I need to pass this to a
function which can handle both (Similar to the Foo function above). I
am not sure how that Foo function has to be defined in MC++?

You'd probably want Object*, since all managed types derive from Object.
That said, you'd need to box value types...
Also an additional question - What exactly is IntPtr? Is it void
equivalent?
Not quite. It is mostly used for interop where you need to handle values of
pointers (possibly pointing to unmanaged memory).
 

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