Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.

Semantics of the this Pointer in Value and Reference Types

When defining types using Visual C++, the this pointer in a reference type is of type handle. The this pointer in a value type is of type interior pointer.

Remarks

One area where the different semantics of the this pointer can result in unexpected behavior is when calling a default indexer. See the example in this topic for an example of the correct way to access a default indexer in value and reference types.

For more information, see

Example

This sample shows how to access a type's default indexer in both reference and value types.

// semantics_of_this_pointer.cpp
// compile with: /clr
using namespace System;

ref struct A {
   property Double default[Double] {
      Double get(Double data) {
         return data*data;
      }
   }

   A() {
      // accessing default indexer
      Console::WriteLine("{0}", this[3.3]);
   }
};

value struct B {
   property Double default[Double] {
      Double get(Double data) {
         return data*data;
      }
   }
   void Test() {
      // accessing default indexer
      Console::WriteLine("{0}", this->default[3.3]);
   }
};

int main() {
   A ^ mya = gcnew A();
   B ^ myb = gcnew B();
   myb->Test();
}

Output

10.89
10.89

See Also

Community Additions

ADD
Show:
© 2015 Microsoft