ref classes and value classes

  • Thread starter news.chi.sbcglobal.net
  • Start date
N

news.chi.sbcglobal.net

Hi.

I am a little confused about the difference between a "value class" and a
"ref class".

I have the following code sample that shows the definition of a value class
and of a ref class and each is instantiated twice - one on the stack and one
on the managed heap. I see no difference between the two, at least not in
the way they are used. Can someone explain the difference, please? Thank
you.

Jeff

ref class RefClass
{
public:
int x;

RefClass(int xx) { x = xx; }
};

value class ValClass
{
public:
int x;

ValClass(int xx) { x = xx; }
};

int main(array<System::String ^> ^args)
{

// create the value class
ValClass val(1);

// use the value class
Console::WriteLine("{0}", val.x);

// create a ref class
RefClass ref(1);

// use the ref class
Console::WriteLine("{0}", ref.x);

// create val on the managed heap
ValClass ^valHandle = gcnew ValClass(1);

// use it
Console::WriteLine("{0}", valHandle->x);

// create ref on the managed heap
RefClass ^refHandle = gcnew RefClass(1);

// use it
Console::WriteLine("{0}", refHandle->x);

return 0;
}
 
G

Guest

Hi,
I have the following code sample that shows the definition of a value class
and of a ref class and each is instantiated twice - one on the stack and one
on the managed heap. I see no difference between the two, at least not in
the way they are used. Can someone explain the difference, please? Thank
you.

ref and value types are always created on the managed heap. not on the stack.

See this page for an overview of the differences between ref / value and
struct / class.
http://msdn2.microsoft.com/en-us/library/6w96b5h7(vs.80).aspx
Also for an explanation of stack semantics:
http://msdn2.microsoft.com/en-us/library/ms177191(VS.80).aspx

--

Kind regards,
Bruno.
(e-mail address removed)
Remove only "_nos_pam"
 
Z

Zephryn Xirdal

msdn2.microsoft.com/en-us/library/ms177191(VS.80).aspx

But that pages doesn't solve differences between ref class and value
class, and I'm interested in that too.

I think the unique difference is that a value class cannot inherit from
other class than an interface, but then I don’t understand the philosophy
of that.
 
G

Gary Chang[MSFT]

Hi Jeff
I am a little confused about the difference between a
"value class" and a "ref class".

All value class types implicitly inherit from the class System::ValueType,
and they can only inherit from zero or more managed interfaces. A value
class is always used to define the primitive data types, such as the
built-in value type int, char. It likes the old plain C style struct. It
would be created in the stack by default, except you use the gcnew to
create it on the managed heap explicitly.

A ref class defines a data structure that contains fields, function members
(functions, properties, events, operators, instance constructors,
destructors, and static constructors), and nested types. Ref classes
support inheritance, they can inherit from zero or more managed interfaces
and zero or one ref types. The ref class likes the C++ style class, and it
is always created on the managed heap.

I hope the above information helps, if you have any questions or concerns,
please do not hesitate to let me know. I am standing by to help you.

Thanks!

Best regards,

Gary Chang
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Guest

But that pages doesn't solve differences between ref class and value
class, and I'm interested in that too.

I think the unique difference is that a value class cannot inherit from
other class than an interface, but then I don’t understand the philosophy
of that.

I think this will be of interest to you. Especially chapter 3.
www.gotw.ca/publications/C++CLIRationale.pdf

--

Kind regards,
Bruno.
(e-mail address removed)
Remove only "_nos_pam"
 
G

Guest

All value class types implicitly inherit from the class System::ValueType,
and they can only inherit from zero or more managed interfaces. A value
class is always used to define the primitive data types, such as the
built-in value type int, char. It likes the old plain C style struct. It
would be created in the stack by default, except you use the gcnew to
create it on the managed heap explicitly.

The documentation on this is ambiguous I think:
http://msdn2.microsoft.com/en-us/library/ke3a209d(VS.80).aspx

at the top of the article it explicitly specifies:
Reference (ref) types and value types can only be instantiated on the
managed heap, not on the stack or on the native heap.

yet the example at the bottom seems to indicate that value types can be put
on the stack. this means that the text at the top of the article is wrong?

--

Kind regards,
Bruno.
(e-mail address removed)
Remove only "_nos_pam"
 
D

Doug Harrison [MVP]

The documentation on this is ambiguous I think:
http://msdn2.microsoft.com/en-us/library/ke3a209d(VS.80).aspx

at the top of the article it explicitly specifies:
Reference (ref) types and value types can only be instantiated on the
managed heap, not on the stack or on the native heap.

yet the example at the bottom seems to indicate that value types can be put
on the stack. this means that the text at the top of the article is wrong?

Yes, it's wrong. Creating a value type with gcnew creates a boxed value
type, as described here:

http://msdn2.microsoft.com/en-us/library/zkch586s(VS.80).aspx

Ordinarily, you wouldn't do this. You'd just declare the value type object
as an ordinary local variable, and it would live on the stack. Contrast
this with reference types, which you can declare as seemingly ordinary
local variables, but which actually live on the GC heap.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top