Visual C++ .NET/Class/value class
Value class
<source lang="csharp">
- include "stdafx.h"
using namespace System; value class Coord3D{ public:
double x; double y; double z; Coord3D (double x, double y, double z) { this->x = x; this->y = y; this->z = z; } String^ Write() { return String::Format("{0},{1},{2}", x, y, z); }
}; void main() {
Coord3D coordA; Coord3D coordB(1,2,3); coordA = coordB; coordA.x += 5.5; coordA.y *= 2.7; coordA.z /= 1.3; Console::WriteLine(coordB.Write()); Console::WriteLine(coordA.x); Console::WriteLine(coordA.y); Console::WriteLine(coordA.z);
}
</source>
value class address
<source lang="csharp">
- include "stdafx.h"
using namespace System; value class MyClass{
int i, j; public: void PrintStartingAddress() { interior_ptr<MyClass> ptr_to_this = this; pin_ptr<MyClass> pinned_this = ptr_to_this; Console::WriteLine("Starting address of object is 0x{0:x}",reinterpret_cast<int>(pinned_this)); }
}; int main() {
MyClass v; v.PrintStartingAddress();
}
</source>
value class demo
<source lang="csharp">
- include "stdafx.h"
value class MyClass {
private: array<double>^ pos; // declare the managed array unsigned int a; unsigned int b; public: void Initialize() { }
}; void main(void) {
int n_atoms = 50; array<MyClass>^ atoms = gcnew array<MyClass>(n_atoms);
for (int i = 0; i < n_atoms; i++) { atoms[i].Initialize( /* ... */ ); }
}
</source>
value class type
<source lang="csharp">
- include "stdafx.h"
value class MyClass {
public: property double MyDouble { double get(); }
}; double MyClass::MyDouble::get() {
return 0;
}
</source>