How to: Test for Equality

JavaScript EditorDhtml editor     Free javascript download 



Main Page

In the following sample, a test for equality using Managed Extensions for C++ is based on what the pointers point to.

For more information, see Breaking Changes in the Visual C++ 2005 Compiler.

Example

В CopyCode imageCopy Code
// mcppv2_equality_test.cpp
// compile with: /clr:oldSyntax /LD
using namespace System;

bool Test1() {
   String * str1 = S"test";
   String * str2 = S"test";
   return (str1 == str2);
}

The IL for this program shows that the return value is implemented with this instruction:

В CopyCode imageCopy Code
  IL_0012:  ceq

which compares the addresses of the two String objects.

Using the new syntax,

В CopyCode imageCopy Code
// mcppv2_equality_test_2.cpp
// compile with: /clr /LD
using namespace System;

bool Test1() {
   String ^ str1 = "test";
   String ^ str2 = "test";
   return (str1 == str2);
}

The IL for this program shows that the return value is implemented with a call to op_Equality.

В CopyCode imageCopy Code
  IL_0012:  call       bool [mscorlib]System.String::op_Equality(string,
                                                                 string)

See Also

Other Resources

Managed Types



JavaScript EditorDhtml editor     Free javascript download