Visual C++ .NET/Class/ref class

Материал из .Net Framework эксперт
Версия от 12:05, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

class, ref class, value class

 
#include "stdafx.h"
using namespace System;
class       ClassMember  {};
ref class   RefClassMember {};
value class ValueClassMember {};
class Class
{
public:
    ValueClassMember  vc;
    ValueClassMember *pvc;
    ClassMember       c;
    ClassMember      *pc;
    int x;
    void write() { Console::WriteLine("Class x: {0}", x); }
};
ref class RefClass
{
public:
    RefClassMember    rc;
    RefClassMember   ^hrc;
    ValueClassMember  vc;
    ValueClassMember ^hvc;
    ValueClassMember *pvc;
    ClassMember      *pc;
    int x;
    void write() { Console::WriteLine("RefClass x: {0}", x); }
};
value class ValueClass
{
public:
    RefClassMember   ^hrc;
    ValueClassMember  vc;
    ValueClassMember ^hvc;
    ValueClassMember *pvc;
    ClassMember      *pc;
    int x;
    void write() { Console::WriteLine("ValueClass x: {0}", x); }
};

class ClassChildClassParent : public Class {};                         
ref class RefClassChildRefClassParent : public RefClass {};            

void main()
{
    Class      _class;
    RefClass   refclass;                            
    ValueClass valueclass;
    RefClass   ^hrefclass    = gcnew RefClass();
    ValueClass ^hvalueclass  = gcnew ValueClass();
    Class      *pclass       = new Class();
    ValueClass *pvalueclass  = & valueclass;
    Class      &rfclass      = *new Class();
    ValueClass &rfvalueclass = valueclass;
    _class.x       = 1;
    refclass.x     = 1;
    valueclass.x   = 1;
    hrefclass->x   = 1;
    hvalueclass->x = 1;
    pclass->x      = 1;
    pvalueclass->x = 1;
    rfclass.x      = 1;
    rfvalueclass.x = 1;
    _class.write();         
    refclass.write();       
    valueclass.write();    
    hrefclass->write();    
    hvalueclass->write();  
    pclass->write();       
    pvalueclass->write();  
    rfclass.write();       
    rfvalueclass.write();  
}


ref class demo

 
#include "stdafx.h"
using namespace System;
public ref class MyClass
{
public:
    MyClass(String^ name, int age)
    {
        Name = name;
        Age = age;
    }
    MyClass(MyClass% myClass)
    {
        *this = myClass;
    }
    virtual String^ ToString() override
    {
        return String::Format("{0}"s Age is {1}", Name, Age);
    }
    void operator = (MyClass% myClass)
    {
        Name = myClass.Name;
        Age = myClass.Age;
    }
private:
    String^ Name;
    int     Age;
};

void myFunc(MyClass param)
{
    Console::WriteLine(%param);
}
void main()
{
    MyClass myClass1("M", 30);
    MyClass myClass2("V", 8);
    myFunc(myClass1);
    myClass1 = myClass2;
    Console::WriteLine(%myClass1);
    MyClass^ myClass3 = %myClass2;
    MyClass myClass4 = *myClass3;
    MyClass% myClass5 = myClass4;
}


ref class with member string variable

 
#include "stdafx.h"
using namespace System;
ref class Hello
{
    String^ greeting;
    public:
    void Greet()
    {
        Console::WriteLine(greeting + "!");
    }
    void SetGreeting(String^ newGreeting)
    {
       greeting = newGreeting;
    }
};
int main()
{
   Hello^ hello = gcnew Hello();
   hello->SetGreeting("Hello World");
   hello->Greet();
   hello->SetGreeting("Howdy");
   hello->Greet();
}


Reference and Indirection

 
#include "stdafx.h"
using namespace System;
ref class RefClass 
{
public:
    int X;
    RefClass(int x)
    {
        X = x;
    }
};

void main()
{
    RefClass rc(10);
    RefClass ^o;
    
    o = %rc;                
    Console::WriteLine(o->X); 
    rc.X = 20;                
    Console::WriteLine(o->X); 
    int %i = rc.X;            
    i = 30;                   
    Console::WriteLine(o->X); 
}


ref type

 
#include "stdafx.h"
using namespace System;
ref class RefType
{
   String^ classname;
   public:
   RefType()
   {
      classname = gcnew String("RefType");
   }
   String^ GetMessage()
   {
       return String::Format("I am a " + classname);
   }
};
int main()
{
   RefType^ r = gcnew RefType();
   Console::WriteLine(r->GetMessage());
}


Stack References

 
#include "stdafx.h"
using namespace System;
ref class Square
{
public:
    int Area()
    {
        return d * d;
    }
    int d;
};
void main()
{
    Square ^sqr1 = gcnew Square();     
    sqr1->d = 2;
    Console::WriteLine( sqr1->Area() );
    Square sqr2;                      
    sqr2.d = 3;
    Console::WriteLine( sqr2.Area() );
}