Nishant S wrote:
void Check(String^% s1, String^% s2)
{
interior_ptr<String^> p1 = &s1;
interior_ptr<String^> p2 = &s2;
Console::WriteLine(p1==p2);
}
void _tmain()
{
String^ s1 = "hello";
String^ s2 = "hello";
Check(s1,s2); //false
Check(s1,s1); //true
}
Wow. May you explain me some things on this code? Because I did not
think that some of these would work.
But let's first convert it to 100% C++/CLI code:
void Check(System::String ^ %s1, System::String ^ %s2)
{
using namespace System;
interior_ptr<String^> p1 = &s1;
interior_ptr<String^> p2 = &s2;
Console::WriteLine(p1==p2);
}
int main()
{
using System::String;
String^ s1 = "hello";
String^ s2 = "hello";
Check(s1, s2); //false
Check(s1, s1); //true
}
Questions:
1) void Check(System::String ^ %s1, System::String ^ %s2)
In this as far as I can understand we pass tracking references of
handles. If we make it
void Check(System::String ^s1, System::String ^s2)
both calls in main return false. Why?
Are new String objects created in the call, although handles are used?
2) I can not understand this interior_ptr use. We create interior
pointers to handles of Strings. That is, the handles themselves cannot
move in memory, while Strings themselves can move.
Also the p1==p2 comparison looks like it is a comparison between two
pointers to handles, and not of two pointers to Strings.
3) As far as I can understand the first call could also return true,
since the compiler could use only one "hello" to do the assignment. As
far as I know at these assignments, no String objects are created, but
the handles point to the string literals themselves, or am I wrong?
--
Ioannis Vranos