473,396 Members | 1,900 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

ref classes and value classes

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;
}





Apr 18 '06 #1
8 2588
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/lib...h7(vs.80).aspx
Also for an explanation of stack semantics:
http://msdn2.microsoft.com/en-us/lib...91(VS.80).aspx

--

Kind regards,
Bruno.
br**********************@hotmail.com
Remove only "_nos_pam"
Apr 18 '06 #2
On Tue, 18 Apr 2006 09:59:06 +0200, Bruno van Dooren
<br**********************@hotmail.com> wrote:
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.
--
Leyendo:
Bolsas de viaje (Verne), alias Beca de viaje, alias Los piratas del Halifax
El vizconde de Braguelonne (Dumas)
-------------------
EstadĂ*sticas BOINC: http://www.boincsynergy.com/images/stats/comb-3905.jpg
-------------------
Un pueblo que quiere ser feliz no ha de menester las conquistas.
-- Plutarco. (50-125) Escritor griego.
Apr 18 '06 #3
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.

Apr 18 '06 #4
> 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.
br**********************@hotmail.com
Remove only "_nos_pam"
Apr 18 '06 #5
> 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/lib...9d(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.
br**********************@hotmail.com
Remove only "_nos_pam"

Apr 18 '06 #6
On Tue, 18 Apr 2006 02:30:01 -0700, Bruno van Dooren
<br**********************@hotmail.com> wrote:
The documentation on this is ambiguous I think:
http://msdn2.microsoft.com/en-us/lib...9d(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/lib...6s(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.

--
Doug Harrison
Visual C++ MVP
Apr 18 '06 #7
Thanks for the replies. I think I have a pretty good understanding now.

I also found the following article interesting and helpful, which talks
about using stack semantics for ref classes.

http://msdn2.microsoft.com/en-us/lib...91(VS.80).aspx
"Doug Harrison [MVP]" <ds*@mvps.org> wrote in message
news:rb********************************@4ax.com...
On Tue, 18 Apr 2006 02:30:01 -0700, Bruno van Dooren
<br**********************@hotmail.com> wrote:
The documentation on this is ambiguous I think:
http://msdn2.microsoft.com/en-us/lib...9d(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/lib...6s(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.

--
Doug Harrison
Visual C++ MVP

Apr 19 '06 #8
On Tue, 18 Apr 2006 11:23:01 +0200, Bruno van Dooren
<br**********************@hotmail.com> wrote:
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

Yes!

Thank you!
--
Leyendo:
Bolsas de viaje (Verne), alias Beca de viaje, alias Los piratas del Halifax
El vizconde de Braguelonne (Dumas)
-------------------
EstadĂ*sticas BOINC: http://www.boincsynergy.com/images/stats/comb-3905.jpg
-------------------
Me temo que todavĂ*a hay bastante machismo soterrado entre los fĂ*sicos.
-- Lee Smolin. FĂ*sico teĂłrico y cosmĂłlogo estadounidense.
Apr 19 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Bob Rock | last post by:
Hello, in the last few days I've made my first few attempts at creating mixed C++ managed-unmanaged assemblies and looking aftwerwards with ILDASM at what is visible in those assemblies from a...
18
by: Edward Diener | last post by:
Is the packing alignment of __nogc classes stored as part of the assembly ? I think it must as the compiler, when referencing the assembly, could not know how the original data is packed otherwise....
14
by: pmclinn | last post by:
I've noticed that many programmers use classes to store data about such things like: Class Customers .....Phone ....ID ....Address End Class....
5
by: Alex | last post by:
Hello, Is it possible to have a class that has "readonly" properties for some classes and can be written by others? The reason I want this is that some of my classes need to expose their...
5
by: Chris Szabo | last post by:
Good afternoon everyone. I'm running into a problem deserializing a stream using the XmlSerializer. A stored procedure returns the following from SQL Server: <Student StudentId="1" Status="1"...
6
by: Philip Potter | last post by:
I have a graph data structure. It goes something like this: class GraphNode { private: friend class Graph; // successor GraphNodes stored as indices of the Graph's array int value; int next1,...
173
by: Zytan | last post by:
I've read the docs on this, but one thing was left unclear. It seems as though a Module does not have to be fully qualified. Is this the case? I have source that apparently shows this. Are...
2
by: Paul Melis | last post by:
Hello, The python library docs read in section 2.1 (http://docs.python.org/lib/built-in-funcs.html): " .... property( ]]]) Return a property attribute for new-style classes (classes that
12
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms....
45
by: =?Utf-8?B?QmV0aA==?= | last post by:
Hello. I'm trying to find another way to share an instance of an object with other classes. I started by passing the instance to the other class's constructor, like this: Friend Class...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.