C++/CLI几点说明:

结构:

// Definition of a struct to represent rectangles
struct RECTANGLE
{
  int Left;                            // Top left point
  int Top;                             // coordinate pair

  int Right;                           // Bottom right point
  int Bottom;                          // coordinate pair
};

类:

class CBox                             // Class definition at global scope
{
  public:
    double m_Length;                   // Length of a box in inches
    double m_Width;                    // Width of a box in inches
    double m_Height;                   // Height of a box in inches
};

 

CBox boxes[5];                       // Array of CBox objects declared
  CBox match(2.2, 1.1, 0.5);           // Declare match box
  CBox cigar(8.0, 5.0, 1.0);           // Declare cigar Box
  CBox* pB1 = &cigar;             // Initialize pointer to cigar object address
  CBox* pB2 = 0;                       // Pointer to CBox initialized to null

  cout << endl
       << "Address of cigar is " << pB1     // Display address
       << endl
       << "Volume of cigar is "
       << pB1->Volume();                    // Volume of object pointed to

  pB2 = &match;
  if(pB2->Compare(pB1))                     // Compare via pointers
    cout << endl
         << "match is greater than cigar";
  else
    cout << endl
         << "match is less than or equal to cigar";

  pB1 = boxes;                              // Set to address of array
  boxes[2] = match;                         // Set 3rd element to match
  cout << endl                              // Now access thru pointer
       << "Volume of boxes[2] is " << (pB1 + 2)->Volume();

  cout << endl;

 

// Class representing a height
value class Height
{
private:
  // Records the height in feet and inches
  int feet;
  int inches;

public:
  // Create a height from inches value
  Height(int ins)
  {
    feet = ins/12;
    inches = ins%12;
  }

  // Create a height from feet and inches
  Height(int ft, int ins) : feet(ft), inches(ins){}
};

 

Height myHeight = Height(6,3);
  Height^ yourHeight = Height(70);
  Height hisHeight = *yourHeight;

  Console::WriteLine(L"My height is {0}", myHeight);
  Console::WriteLine(L"Your height is {0}", yourHeight);
  Console::WriteLine(L"His height is {0}", hisHeight);

 

ref class Box 
{
  public:
    // No-arg constructor supplying default field values
    Box(): Length(1.0), Width(1.0), Height(1.0)
   {
     Console::WriteLine(L"No-arg constructor called.");
   }
    // Constructor definition using an initialisation list
    Box(double lv, double bv, double hv):
                             Length(lv), Width(bv), Height(hv)
   {
     Console::WriteLine(L"Constructor called.");
   }

   // Function to calculate the volume of a box
   double Volume()
   {
     return Length*Width*Height;
   }

  private:
    double Length;                     // Length of a box in inches
    double Width;                      // Width of a box in inches
    double Height;                     // Height of a box in inches
};

 

 

Box^ aBox;                           // Handle of type Box^
  Box^ newBox = gcnew Box(10, 15, 20);
  aBox = gcnew Box;                    // Initialize with default Box
  Console::WriteLine(L"Default box volume is {0}", aBox->Volume());
  Console::WriteLine(L"New box volume is {0}", newBox->Volume());

 

 

属性的定义:

 

using namespace System;

ref class Name
{
  private:
  array<String^>^ Names;

public:
  Name(...array<String^>^ names) : Names(names) {}

  // Scalar property specifying number of names
  property int NameCount
  {
    int get() {return Names->Length; }
  }

  // Indexed property to return names
  property String^ default[int]
  {
    String^ get(int index)
    {
      if(index >= Names->Length)
        throw gcnew Exception(L"Index out of range");
      return Names[index];
    }

    void set(int index, String^ name)
    {
      if(index >= Names->Length)
        throw gcnew Exception(L"Index out of range");
      Names[index] = name;
    }
  }

  // Indexed property to return initials
  property wchar_t Initials[int]
  {
    wchar_t get(int index)
    {
      if(index >= Names->Length)
        throw gcnew Exception(L"Index out of range");
      return Names[index][0];
    }
  }
};

int main(array<System::String ^> ^args)
{
  Name^ myName = gcnew Name(L"Ebenezer", L"Isaiah", L"Ezra", L"Inigo",
                                                               L"Whelkwhistle");
  myName[myName->NameCount - 1] = L"Oberwurst";  // Change last indexed property

  // List the names
  for(int i = 0 ; i < myName->NameCount ; i++)
    Console::WriteLine(L"Name {0} is  {1}", i+1, myName[i]);

  // Output the initials
  Console::Write(L"The initials are:");
  for(int i = 0 ; i < myName->NameCount ; i++)
    Console::Write(L" {0}", myName->Initials[i]);
  Console::WriteLine();

  return 0;
}

1、定义标量属性

原文链接: https://www.cnblogs.com/panxihua/archive/2012/06/11/1865884.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    C++/CLI几点说明:

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/52467

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月9日 上午3:55
下一篇 2023年2月9日 上午3:55

相关推荐