Export (0) Print
Expand All

20 Managed Operators

Visual Studio .NET 2003

The Managed Extensions have several kinds of managed operators. They are discussed in Section 20.1 and Section 20.2.

Operators work as expected on __value classes using infix notation. However, the distinguished name must be used to call operators on __gc classes. This occurs because operators take parameters with pointer type for __gc classes, but user-defined operators cannot be called on pointer types in C++.

Example

// mcpp__static_class_ctors2.cpp
// compile with: /clr
#using <mscorlib.dll>

__gc class G {
   int i;
public:
   static bool op_Inequality(G* g1, G* g2) {
      return g1->i != g2->i;
   }
};

int main() {
   G* g1 = new G;
   G* g2 = new G;

   // ok: direct call
   bool b1 = G::op_Inequality(g1, g2);

   // ok: but does pointer comparison, only
   bool b2 = (g1 != g2);      

   // error: no operator that takes two objects as parameters
   // bool b3 = (*g1 != *g2 );   
}

Operators for value classes can pass __gc parameters by reference even though this is not compliant with the Common Language Specification. To ensure interoperability, the programmer should check conformance of his or her code with the Common Language Specification.

Show:
© 2014 Microsoft