<@乌龟:>C++/CLI语言Specification阅读笔记(2)

本笔记主要包含以下的内容:

1.空指针nullptr
2.自动内存管理(GC)
3.托管
4.标准的属性
5.带序列的属性(数组式的访问方法)

1.空指针nullptr
using namespace System;

int main()
{
    Object
^ obj1 = nullptr;
    Object
^ obj2 = 0;          //performance Boxing
    String^ str1 = nullptr;

    
if (obj1 == 0);            //false, 0 is boxed
    if (obj1 == nullptr);      //true

    
char* pc1 = nullptr;
    
if (pc1 == 0);             //true, 0 is a null pointer value
    if (pc1 == nullptr);       //true, nullptr is a null pointer constant

    
int n1 = 0;
    n1 
= nullptr;             //error, no implicit conversion
    if (n1 == 0);             //true
}
2.自动内存管理
using namespace System;

public ref class Stack
{
public:
    Stack()
    {
        first 
= nullptr;
    }

    property 
bool  IsEmpty
    {
        
bool get()
        {
            
return (first == nullptr);
        }
    }

    Object
^ Pop()
    {
        
if (first == nullptr)
        {
            
throw gcnew Exception("Can't Pop from an empty stack");
        }
        
else
        {
            Object
^ temp = first->value;
            first 
= first->next;
            
return temp;
        }
    }

    
void Push(Object^ o)
    {
        first 
= gcnew Node(o, first);
    }

    
ref struct Node
    {
        Node
^ next;
        Object
^ value;

        Node(Object
^ val)
        {
            next 
= nullptr;
            value 
= val;
        }

        Node(Object
^ val, Node^ nex)
        {
            next 
= nex;
            value 
= val;
        }
    };
private:
    Node
^ first;
};

int main()
{
    Stack
^ s = gcnew Stack;

    
for (int i = 0;  i < 10; i ++)
    {
        s
->Push(i);
    }

    
for (int i = 0; i < 10; i ++)
    {
        Object
^ obj = s->Pop();    
        
int j = safe_cast<int>(obj);
        Console::WriteLine(j);
    }
    s 
= nullptr;
}

3.委托类型
using namespace System;

delegate void MyFunction(int value);

public ref struct A
{
    
static void F(int i)
    {
        Console::WriteLine(
"F:{0}",i);
    }
};

public ref struct B
{
    
void G(int i)
    {
        Console::WriteLine(
"G:{0}",i);
    }
};

int main()
{
    MyFunction
^ d;
    d 
= gcnew MyFunction(&A::F);
    d(
10);
    
    B
^ b = gcnew B;
    d 
+= gcnew MyFunction(b, &B::G);
    d(
20);

    d 
+= gcnew MyFunction(&A::F);
    d(
30);

    d 
-= gcnew MyFunction(b, &B::G);
    d(
40);
}

4.标准的属性
using namespace System;

public value class Point
{
    
int x;
    
int y;

public:
    property 
int X
    {
        
int get() { return x; }
        
void set(int value) { x = value; }
    }
    
    property 
int Y
    {
        
int get() { return y; }
        
void set(int value) { y = value; }
    }

    Point(
int x, int y)
    {
        Move(x,y);
    }

    
void Move(int nx, int ny)
    {
        x 
= nx;
        y 
= ny;
    }

    
void Translate(int dx, int dy)
    {
        x 
+= dx;
        y 
+= dy;
    }
};

int main()
{
    Point p1;
    p1.X 
= 10;
    p1.Y 
= 5;
    p1.Move(
57);
    Point p2(
91);
    p2.Translate(
-412);
}

5.带序列的属性
using namespace System;

public ref class Stack
{
public:
    
ref struct Node
    {
    
public:
        Node
^ next;
        Object
^ value;
        Node(Object
^ value) : next(nullptr), value(value) {}
        Node(Object
^ val, Node^ nxt)
        {
            value 
= val;
            next 
= nxt;
        }
    };
    
private:
        Node
^ first;
        Node
^ GetNode(int index)
        {
            Node
^ temp = first;
            
while (index > 0)
            {
                temp 
= temp->next;
                index
--;
            }
            
return temp;
        }
        
bool ValidIndex(int index)
        {
            
//add code
            return true;
        }
public:
    property Object
^ default[int]
    {
        Object
^ get(int index)
        {
            
if (!ValidIndex(index))
            {
                
throw gcnew Exception("Index out of range");
            }
            
else
            {
                
return GetNode(index)->value;
            }
        }
        
void set(int index, Object^ value)
        {
            
if (!ValidIndex(index))
            {
                
throw gcnew Exception("Index out of range");
            }
            
else
            {
                GetNode(index)
->value = value;
            }
        }
    }

    Object
^ Pop()
    {
        
//add code
        return nullptr;
    }

    
void Push(Object^ o)
    {
        
//add code
    }
};







posted on 2009-07-29 16:54  乌龟_毛驴  阅读(250)  评论(0编辑  收藏  举报

导航