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
Description
This sample shows how to access a type's default indexer in both reference and value types.
Code
// 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