confuse about ref class / value class in declare variable

  • Avatar of bitfish_jzl
    bitfish_jzl 
    ref class RefSquare
    {
    public:
    int Area(){return Dims * Dims;}
    int Dims;
    };
    value class ValSquare
    {
    public:
    int Area(){return Dims * Dims;}
    int Dims;
    };
    class OldSquare
    {
    public:
    int Area(){return Dims * Dims;}
    int Dims;
    };
    void testFunction()
    {
      RefSquare ^Rsqr1=gcnew RefSquare();
      RefSquare Rsqr2;
      ValSquare* Rsqr3 = new ValSquare();
      ValSquare ^Vsqr1=gcnew ValSquare();
      ValSquare Vsqr2;
      OldSquare *OldSqr1=new OldSquare();
      OldSquare OldSqr2;  Rsqr1->Dims=3;
      Rsqr2.Dims=4;
      Vsqr1->Dims=3;
      Vsqr2.Dims=4;
      OldSqr1->Dims=3;
      OldSqr2.Dims=4;     /* do some stuff here */
    } 
    

    I am learning C++/CLI.
    as the books speak:
    "ref class" return a return a reference that store in managed heap; "value class" return a value that store in stack just like "int".
    then in variable declaration, there comes a confusion to me.
    other than the storage managed heap or stack, is it affect the using of variable?
    literally, It looks like:
        "ref class" object return a hadle when creating, and invoke object's member using "->"
        "value class" object return a value when creating, and invoke  object's member using "."
    But according the codes on the top:
        "ref class" can decare a variable like "Rsqr2".
        "value class" can decare a variable like "Vsqr1".
    Does "ref class" and "value class" not affect the using of variable?
    Are they only difference is storage managed heap or stack?

    Please give me a hand.  ^^
    • Moved bySheng Jiang 蒋晟MVPSaturday, March 19, 2011 5:34 PM (From:Visual C++) 

Answers

  • Saturday, March 19, 2011 5:33 PM
    Avatar of Sheng Jiang 蒋晟
    Sheng Jiang 蒋晟
    (MVP)
    100,655

    RefSquare ^Rsqr1=gcnew RefSquare(); //create a tracking handle of a reference class object and store it in the declared variable Rsqr1

    RefSquare Rsqr2;//declare a tracking handle of a reference class object using special stack-based notation whose lifetime is associated within the scope of the declaration

    ValSquare ^Vsqr1=gcnew ValSquare(); //create a value class object on the stack, then create a boxing on the heap and store itstracking handle in the declared variable Vsqr1

    ValSquare Vsqr2; //create a value class object on the stack

    OldSquare *OldSqr1=new OldSquare(); //create a native class object on the heap  and store it in the declared variable OldSqr1

    OldSquare OldSqr2; //create a native class object on the stack

    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    • Marked As Answer byYi Feng LiMicrosoft Contingent Staff, ModeratorTuesday, March 29, 2011 3:15 AM

All Replies

  • Saturday, March 19, 2011 5:33 PM
    Avatar of Sheng Jiang 蒋晟
    Sheng Jiang 蒋晟


    RefSquare ^Rsqr1=gcnew RefSquare(); //create a tracking handle of a reference class object and store it in the declared variable Rsqr1

    RefSquare Rsqr2;//declare a tracking handle of a reference class object using special stack-based notation whose lifetime is associated within the scope of the declaration

    ValSquare ^Vsqr1=gcnew ValSquare(); //create a value class object on the stack, then create a boxing on the heap and store itstracking handle in the declared variable Vsqr1

    ValSquare Vsqr2; //create a value class object on the stack

    OldSquare *OldSqr1=new OldSquare(); //create a native class object on the heap  and store it in the declared variable OldSqr1

    OldSquare OldSqr2; //create a native class object on the stack

    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    • Marked As Answer byYi Feng LiMicrosoft Contingent Staff, ModeratorTuesday, March 29, 2011 3:15 AM

 

另:

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


 

你可能感兴趣的:(confuse about ref class / value class in declare variable)