Visual C++ .NET/Class/value class

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Value class

 
#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);
}


value class address

 
#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();
}


value class demo

 
#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( /* ... */ );
   }
}


value class type

 
#include "stdafx.h"
value class MyClass
{
   public:
   property double MyDouble
   {
      double get();
   }
};
double MyClass::MyDouble::get()
{
   return 0;
}