Export (0) Print
Expand All
0 out of 2 rated this helpful - Rate this topic

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

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.