none
ref class VS. value class

    Question

  • HI everyone,

    I am new in C++.NET 2005 world and there is something that makes me a little crazy:

    What is the difference between ref class and value class also ref struct and value struct?

    Which type of classes (ref or value) I should create for my applications?

    I hope anyone who have the answer helps me to figure this out of me.

    Sunday, November 12, 2006 11:18 PM

Answers

  • It was weekend - so be patient with us ;)

    class and struct are just different in that class members are private by default and struct are public by default.

    ref class or struct will create reference types. They are created on the managed heap and only references (like pointers when you think of it natively) to those objects are stored and passed.

    value class or struct are value types. When you pass them around as parameters or as members the whole memory block will be transferred. So you sould use value types only for small data types.

    Your default should always be ref class, only for quite simple types like Point or Color in the framework you would choose value struct.

    --
    SvenC

    Monday, November 13, 2006 8:19 AM
  • Regarding class vs struct, as in the non-.net C++ world there is no real reason for having both.  I've always found it mystifying that both are included when the only difference is the default access level (I've heard that the reason is to accommodate migration from C, but that's a pretty lame reason for the extra baggage given search/replace).  So, you're probably better off to forget about "struct" altogether and focus on the difference between "ref class" and "value class" (as the others have explained so well).

    If you're familiar with C#, additional confusion arises due to C# having a "struct" which is identical to the C++/CLI "value class", not the C++/CLI "struct".

    David Anton
    www.tangiblesoftwaresolutions.com
    Instant C#: VB to C# converter
    Instant VB: C# to VB converter
    Instant C++: C# to C++ converter, VB to C++ converter
    Instant Python: VB to Python converter

    Monday, November 13, 2006 3:36 PM
  • No one wants to help me!! with this little issue that considered a basic level in the MC++ world

    I am really disappointed :-(

    Monday, November 13, 2006 12:51 AM
  • I'm sorry that you feel neglected. Some questions just slip by unnoticed, while we're all sleeping :)

    The main difference between ref and value types is that ref types contain pointers to their data, while value types contain the actual data. For most uses, that is worker classes, gui classes, utility classes, you'll want to use the ref types. If, on the other hand, you're writing a simple class or structure which wraps e.g. a double you can use the value type. The rule of thumb is to use value class/struct whenever a class' main task is to logically wrap a single or multiple of managed values.

    Monday, November 13, 2006 8:28 AM
    Moderator

All replies

  • No one wants to help me!! with this little issue that considered a basic level in the MC++ world

    I am really disappointed :-(

    Monday, November 13, 2006 12:51 AM
  • It was weekend - so be patient with us ;)

    class and struct are just different in that class members are private by default and struct are public by default.

    ref class or struct will create reference types. They are created on the managed heap and only references (like pointers when you think of it natively) to those objects are stored and passed.

    value class or struct are value types. When you pass them around as parameters or as members the whole memory block will be transferred. So you sould use value types only for small data types.

    Your default should always be ref class, only for quite simple types like Point or Color in the framework you would choose value struct.

    --
    SvenC

    Monday, November 13, 2006 8:19 AM
  • I'm sorry that you feel neglected. Some questions just slip by unnoticed, while we're all sleeping :)

    The main difference between ref and value types is that ref types contain pointers to their data, while value types contain the actual data. For most uses, that is worker classes, gui classes, utility classes, you'll want to use the ref types. If, on the other hand, you're writing a simple class or structure which wraps e.g. a double you can use the value type. The rule of thumb is to use value class/struct whenever a class' main task is to logically wrap a single or multiple of managed values.

    Monday, November 13, 2006 8:28 AM
    Moderator
  • Oops, I went to make some coffee after hitting the reply button. That spells out double effort :)
    Monday, November 13, 2006 8:30 AM
    Moderator
  • So at least the OP has got doubled attention ;)
    Monday, November 13, 2006 8:33 AM
  • Thanks SvenC and einaros

    I am really happy because there are people like you in the world ;-)

    • Proposed as answer by ok_ranger Tuesday, May 19, 2009 9:12 PM
    Monday, November 13, 2006 12:43 PM
  • Regarding class vs struct, as in the non-.net C++ world there is no real reason for having both.  I've always found it mystifying that both are included when the only difference is the default access level (I've heard that the reason is to accommodate migration from C, but that's a pretty lame reason for the extra baggage given search/replace).  So, you're probably better off to forget about "struct" altogether and focus on the difference between "ref class" and "value class" (as the others have explained so well).

    If you're familiar with C#, additional confusion arises due to C# having a "struct" which is identical to the C++/CLI "value class", not the C++/CLI "struct".

    David Anton
    www.tangiblesoftwaresolutions.com
    Instant C#: VB to C# converter
    Instant VB: C# to VB converter
    Instant C++: C# to C++ converter, VB to C++ converter
    Instant Python: VB to Python converter

    Monday, November 13, 2006 3:36 PM
  • Thanks David Anton

    As you said I am already familiar with C# and what you have said about "C# struct" == "C++/CLI value class" and "C# class" =="C++/CLI ref class" is really a great addition for me.

    Thanks again :-)

    Monday, November 13, 2006 4:22 PM