Daniel Wilson
asked on
C++/CLI -- address of operator
Consider this code in Managed Extensions for C++ (.Net 1.1)
int MyInt;
MyFunction(&MyInt);
That calls MyFunction with the address of MyInt, because MyFunction wants an int*.
Now, in .Net 2+, MyFunction wants an int^.
So ... how do I call it? If I say
MyFunction(%MyInt);
I am told Error C3071: % can only be applied to an instance of a ref class or a value type.
I would have thought that int was a value type.
So again, how do I call MyFunction?
Thanks!
int MyInt;
MyFunction(&MyInt);
That calls MyFunction with the address of MyInt, because MyFunction wants an int*.
Now, in .Net 2+, MyFunction wants an int^.
So ... how do I call it? If I say
MyFunction(%MyInt);
I am told Error C3071: % can only be applied to an instance of a ref class or a value type.
I would have thought that int was a value type.
So again, how do I call MyFunction?
Thanks!
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I think what the function wants is a handle to an Int32^. I may be wrong, but the following demonstration should be along the lines you're looking for:
System::Int32 someInt;
MyFunction(%someInt);
ASKER
MyFunction(^%MyInt);
does not compile. In addition to C3071, I get:
C2100: Illegal indirection
C3192: Syntax Error: '^' is not a prefix operator. (Did you mean '*' ?)
does not compile. In addition to C3071, I get:
C2100: Illegal indirection
C3192: Syntax Error: '^' is not a prefix operator. (Did you mean '*' ?)
ASKER
System::Int32 someInt;
MyFunction(%someInt);
This doesn't compile either. I still get C3071.
Thanks to both of you for your ideas. Do you have more I can try?
MyFunction(%someInt);
This doesn't compile either. I still get C3071.
Thanks to both of you for your ideas. Do you have more I can try?
ASKER
Another overload out there for MyFunction is
static void MyFunction(System::IntPtr x);
I do find that the call MyFunction(MyInt); compiles. Will I be sorry if I go with this?
static void MyFunction(System::IntPtr x);
I do find that the call MyFunction(MyInt); compiles. Will I be sorry if I go with this?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
>>Well, have you tried just using the address-of operator?
& ? Yes, I started there. But I get C2665: 'MyFunction': none of the X overloads could convert all the argument types
Of course, MyFunction is not really mine. I'm working against the Tao Framework which is written in C#.
One of the functions is this one, the 2nd parameter being my problem one:
public static extern int ChoosePixelFormat(IntPtr deviceContext, ref PIXELFORMATDESCRIPTOR pixelFormatDescriptor);
Another is this, again the 2nd parameter being the one in question.
public static
void glGenTextures(Int32 n, [Out] IntPtr textures)
& ? Yes, I started there. But I get C2665: 'MyFunction': none of the X overloads could convert all the argument types
Of course, MyFunction is not really mine. I'm working against the Tao Framework which is written in C#.
One of the functions is this one, the 2nd parameter being my problem one:
public static extern int ChoosePixelFormat(IntPtr deviceContext, ref PIXELFORMATDESCRIPTOR pixelFormatDescriptor);
Another is this, again the 2nd parameter being the one in question.
public static
void glGenTextures(Int32 n, [Out] IntPtr textures)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
And in real C++, it has no other meaning, to my knowledge.
But in C++/CLI it is similar to &. See the "tracking reference" section here:
http://en.wikipedia.org/wiki/C%2B%2B/CLI