473,320 Members | 1,965 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

CLI/C++ Managed pointer to value type (e.g. Int32)?

Hi, I have a function:

void MyFunction(Int32^ outVar)
{
if (outVar != nullptr)
{
*outVar = 32;
}
}

and code:

{
Int32 emptyVar;
MyClass->MyFunction(emptyVar);
}
a) Why does this compile, when I am not actually passing the address of
the variable through to the function?
b) Is that the correct way of doing it? I tried:

MyClass->MyFunction(&emptyVar);

But it moans about converting it from Int32* to Int32^.

Any tips welcome,

Cheers,
Dec 5 '06 #1
4 2338
David Anton wrote:
For value types, use the "%" symbol for ref parameters:
void MyFunction(Int32% outVar)

I don't know why your original sample compiles though.
I want the parameter to be optional, i.e. the developer can pass nullptr
through as one of the parameters (in my function i have 2 parameters)

void MyFunction(Int32^ param1, Int32^ param2)
{
if (param1 != nullptr)
....

if (param2 != nullptr)
....

}
Dec 5 '06 #2
Mark Ingram wrote:
I want the parameter to be optional, i.e. the developer can pass nullptr
through as one of the parameters (in my function i have 2 parameters)

void MyFunction(Int32^ param1, Int32^ param2)
{
if (param1 != nullptr)
You can not do that. Int32 is a value type, and you can't get a
reference handle to a value type. You can only do a tracking reference,
Int32%.

The problem with your function is that when you take the address of an
integer, the compiler creates a temporary object and passes that to
MyFunction. Pseudo code:

ref struct Int32Wrapper
{
int value;
};

int value;
Int32Wrapper^ value_wrapper = gcnew Int32Wrapper;
value_wrapper->value = value;
MyFunction(value_wrapper);

As you see, when you pass your integer to MyFunction, the compiler
creates a copy of your integer, and MyFunction modifies that copy, while
your original integer gets unchanged.

You have to use Int32% or int%.

Tom
Dec 5 '06 #3
Tamas Demjen wrote:
>
As you see, when you pass your integer to MyFunction, the compiler
creates a copy of your integer, and MyFunction modifies that copy, while
your original integer gets unchanged.

You have to use Int32% or int%.

Tom

Hi, thanks for the input, can I not change my function to:

MyFunction(interior_ptr<Int32param1, interior_ptr<Int32param2)
{
if (param1 != nullptr)
{
}

if (param2 != nullptr)
{
}
}

....

MyFunction(&int1, &int2);

?

Thanks,
Dec 6 '06 #4
Hi, thanks for the input, can I not change my function to:
>
MyFunction(interior_ptr<Int32param1, interior_ptr<Int32param2)
{
if (param1 != nullptr)
{
}

if (param2 != nullptr)
{
}
}
In the C++/CLI standard, I have not found a statement regarding a standard
conversion from int* to interior_ptr<int>,
however, Visual C++ 2005 implements this conversion. Therefore, yes, you can
do this in VC++ 2005.

If you want to be interoperable with other .NET languages, you should not
use interior_ptr, but Nullable<intinstead.

Marcus

Dec 6 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Jason | last post by:
I have a function (Inet_ntop) that returns const char * and if I try to assign that return value to a char * variable, I get the gcc error message: warning: assignment discards qualifiers from...
12
by: Charlie Zender | last post by:
Hi, I am unable to compile a large body of code with extremely pedantic compile time checks activate, so that warnings cause errors. With GCC 3.3.1, I do this with gcc -std=c99 -pedantic...
1
by: farseer | last post by:
How can i do this? i have the following in a config file: name="id" type="int32" can i create a variable called "id" as type int32?
16
by: Abhishek | last post by:
why do I see that in most C programs, pointers in functions are accepted as: int func(int i,(void *)p) where p is a pointer or an address which is passed from the place where it is called. what...
1
by: Dave Parry | last post by:
Hi, I would like to hear peoples thoughts on passing a value type by reference and then trying to store this ref in a member so it can be updated elsewhere, whereby I want the referenced value...
33
by: Ney André de Mello Zunino | last post by:
Hello. I have written a simple reference-counting smart pointer class template called RefCountPtr<T>. It works in conjunction with another class, ReferenceCountable, which is responsible for the...
3
by: Rich | last post by:
Another noob question for you all . . . I have functions written in C++ that I want to call from C#. I need to be able to pass a reference to a value type (like an int) so that the function can...
3
by: .rhavin grobert | last post by:
guess you have the following: _________________________________________________ template <class T> class CQVector { public: // find an element, returns index or -1 if none is found int...
2
by: =?Utf-8?B?c2FtZWVt?= | last post by:
Dear All up to my level that I have known the StringBuilder class is reference type. Someone telling that StringBuilder class is Value type. Hence I want to clarify which is right? and how? ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.