Results 1 to 7 of 7

Thread: [RESOLVED] Some basic questions abut C++/CLI

  1. #1

    Thread Starter
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Resolved [RESOLVED] Some basic questions abut C++/CLI

    Hello to everyone, I am trying to get to know C++/CLI and I have some questions. Just to let you know I have VB.Net background and I also have taken some C++ classes some years before. I hope someone can shade a light on my questions. Here it goes:

    First, what is exactly the deference between the reference class and the value class? I mean why we need to declare the “ref” or “value” classes if we pass the arguments as ref or value in VB.Net. It is a little bit confusing for me to have a type as referenced and value.

    Second, what is this [int main(array<System::String ^> ^args)] line representing? My guess is that the argument is a string array and it may contain the command line for this app passed when the app starts. Is this right?
    Also I have a question about “^”. What I read from MSDN the symbol “^” directs the compiler to create handle for the object in the managed garbagecollecter heap so that it can be cleaned when there are no references to this object. Is this right? And also why they are two on that line? I mean one is not enough having it in front of “args”? Is it because the string data type is an array of chars in C++ and it its self should have a handle in the heap? Is this correct?

    The questions may be very simple but I tried to get the answers on MSDN and still they are confusing for me. I would appreciate for any help.
    Thanks in advance,
    VBDT

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Some basic questions abut C++/CLI

    Quote Originally Posted by VBDT
    First, what is exactly the deference between the reference class and the value class? I mean why we need to declare the “ref” or “value” classes if we pass the arguments as ref or value in VB.Net. It is a little bit confusing for me to have a type as referenced and value.
    A value type variable is a variable that stores a value, such as an integer or a double. A reference type variable points to a memory block where the data is located, thus if you send this variable to another method for instance, any modifications done on it will also be done on the original variable, as they point to the same memory reference.
    Quote Originally Posted by VBDT
    Second, what is this [int main(array<System::String ^> ^args)] line representing? My guess is that the argument is a string array and it may contain the command line for this app passed when the app starts. Is this right?
    Thats correct, it is a managed string array that will contain any arguments passed to the executable.
    Quote Originally Posted by VBDT
    Also I have a question about “^”. What I read from MSDN the symbol “^” directs the compiler to create handle for the object in the managed garbagecollecter heap so that it can be cleaned when there are no references to this object. Is this right?
    That is also correct
    Quote Originally Posted by VBDT
    And also why they are two on that line? I mean one is not enough having it in front of “args”? Is it because the string data type is an array of chars in C++ and it its self should have a handle in the heap? Is this correct?
    The reason is that the array class is a managed type and needs to be handled by the managed garbagecollector, the same goes for the System::String managed class.

    You'll see that with unmanaged classes you do not use the ^, as those are not handled by the garbagecollector.

    Last edited by Atheist; Sep 8th, 2008 at 03:22 PM.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3

    Thread Starter
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: Some basic questions abut C++/CLI

    Thank you Atheist, you defenatly cleard some confusion I had. One more question I would like to ask. As what I read about “ref class” and “value class” it appears that this are only used in C++/CLI projects and this keywords specify that this classes (data types) are managed and these keywords are not used in native C++ code. Is this the case?

  4. #4
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: Some basic questions abut C++/CLI

    There is no such thing as a value class.

    There are value types and reference types. The terminology is important. Classes are reference types and structs are value types.
    I don't live here any more.

  5. #5

    Thread Starter
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: Some basic questions abut C++/CLI

    Quote Originally Posted by wossname
    There is no such thing as a value class.

    There are value types and reference types. The terminology is important. Classes are reference types and structs are value types.
    Yes, you are right! I just put “value class” because this is how they are declared, but you are right about the terminology since I think I understand now what these keywords stand for. So in simple words value types are structure types comparing to VB.Net. And the reference types are the classes compared to VB.Net.

    Thank you for pointing it out!

  6. #6
    Fanatic Member
    Join Date
    Jan 2006
    Posts
    710

    Re: Some basic questions abut C++/CLI

    Quote Originally Posted by wossname
    There is no such thing as a value class.

    There are value types and reference types. The terminology is important. Classes are reference types and structs are value types.
    In C++/CLI, "ref class" and "ref struct" are reference types. "value class" and "value struct" (and "enum class") are value types. You can't make the same distinction that you can make in C#.

    C++/CLI carries on the long-standing C++ tradition of virtually no distinction between 'class' and 'struct' (unlike C#, the only distinction is the default access modifier of the members).
    David Anton
    Convert between VB, C#, C++, & Java
    www.tangiblesoftwaresolutions.com

  7. #7
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: [RESOLVED] Some basic questions abut C++/CLI

    Fair enough, some features of C++ are more dubious than others though. I'd regard value classes as a bit of a hack.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width