none
"ref class or value class" RRS feed

  • Question

  • Hi Igor and everybody

    Can you please tell me when "ref class" or "ref value" is used and what they are. If you could give me simple examples, I would appreciate them.  I have explored MSDN sites for 45 minutes and am not any wiser.

    Regards

    Chong


    Thursday, March 6, 2014 4:17 PM

Answers

  • Hmm, I bet that you used "ref gcnew". It's just gcnew, without any ref in front of it.
    Monday, March 10, 2014 1:23 PM
    Moderator
  • Hi Mike

    Could you please tell me what "ref class" does in your example program?

    Regards

    Chong

    (Sorry, I missed that you had used 'ref new' in your previous post.)

    A 'ref class' is a different kind of class from a regular C++ class. Because it is different, it has to have a different notation. In C++/CLI there is also 'value class'.

    Unless you have a specific reason to learn it, I would forget about C++/CLI for now.


    David Wilkinson | Visual C++ MVP

    Monday, March 10, 2014 3:42 PM
  • There are 2 different language extensions:

    1. C++/CLI - this targets the .NET runtime
    2. C++/CX - this targets the WinRT runtime

    There are a lot of syntax similarities between those 2 extensions but there are also a few differences. Probably the 'ref new'/'gcnew' is the biggest difference when it comes to syntax, 'ref new' is for C++/CX and 'gcnew' is for C++/CLI. The example I have posted is for C++/CLI so you have to use 'gcnew', not 'ref new'.

    I've posted a C++/CLI example because it's easier to play with it. You simply create a CLR Console Application, paste the code in it and run it.

    To play with C++/CX you need to create a Windows Store application. That's easy to do since the wizard generates all the necessary startup code. But these applications are basically GUI applications and in order to be able do something meaningful you also have to learn a bit of XAML.

    Monday, March 10, 2014 3:54 PM
    Moderator

All replies

  • Those are CLI/CX extensions to the C++ language - http://msdn.microsoft.com/en-us/library/6w96b5h7.aspx

    • For C++/CLI "ref class" is a .NET reference type and it is allocated on the garbage collected heap
    • For C++/CX "ref class" is a WinRT class and its lifetime is automatically managed via reference counting

    There's also "value class" (not "ref value" like you said). Unlike "ref class" which are always passed by reference value class objects are usually copied and normally they're not allocated on the garbage collected heap/reference counted.

    Thursday, March 6, 2014 5:00 PM
    Moderator
  • Hi Mike

    I got a hint from your website and written the following simple program.  It does not even compile  :(2 compilation errors). Please correct the program:

    #include "stdafx.h"
    //#include <string.h>
    #include "iostream"

    using namespace std;

    namespace Kim{
     ref class Name sealed{//??
       char* name; 
     public:
       Name(char *s){name=char[20];strcpy_s(name,7,s);}
       void PrintName(){std::{cout<<name<<"\n";}
     };
    }
    //using namespace Kim;

    int main()
    {
     Kim::.Name a("Chong");

     a.PrintName();

     return 1;

    }

    I get the following errors:

    1>  ConsoleApplication1.cpp

    1>c:\users\chong_2\documents\my document(fd1)\temp\c++\consoleapplication1\consoleapplication1.cpp(10): error C2143: syntax error : missing ';' before '<class-head>'

    1>c:\users\chong_2\documents\my document(fd1)\temp\c++\consoleapplication1\consoleapplication1.cpp(10): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

    Thanks

    Chong




    Sunday, March 9, 2014 5:44 PM
  • I got a hint from your website and written the following simple program.  It does not even compile : (2 compilation errors).


    Did you create a CLR Console Application?

    David Wilkinson | Visual C++ MVP

    Sunday, March 9, 2014 8:43 PM
  • In addition to what David Wilkinson already said that code has a few basic syntax errors, C++ or C++/CLI it wouldn't compile anyway. It's also rather pointless since you've made a ref class but you're still using standard C++ features such as iostream and C style strings. Here's a valid C++/CLI version of that code:

    using namespace System;
    
    namespace Kim {
        ref class Name sealed {
            String ^name;
        public:
            Name(String ^s) : name(s) {
            }
            void PrintName() {
                Console::WriteLine(name);
            }
        };
    }
    
    int main() {
        Kim::Name a("Chong");
        a.PrintName();
        return 1;
    }
    

    Monday, March 10, 2014 5:49 AM
    Moderator
  • Hi Mike

    Thanks for helping me. I add the following 2 lines to main() and I get a compilation error:

      Kim::Name ^p;

      p = ref new Kim::Name("Smith"); //

    //error C2065: 'ref' : undeclared identifier

    Regards

    Chong

    Monday, March 10, 2014 11:04 AM
  • Hi Mike

    Thanks for helping me. I add the following 2 lines to main() and I get a compilation error:

      Kim::Name ^p;

      p = ref new Kim::Name("Smith"); //

    //error C2065: 'ref' : undeclared identifier

    Regards

    Chong

    As I already told you, you must create a CLR console application, not a Win32 console application. Just start over.

    Note that you must be using VS2005 or later in order to use C++/CLI.

    Why to you want to start learning C++/CLI? I would recommend mastering standard C++ first. C++/CLI is a very complex language.


    David Wilkinson | Visual C++ MVP

    Monday, March 10, 2014 11:18 AM
  • In C++/CLI it's gcnew:

    Kim::Name ^p = gcnew Kim::Name("Smith");

    "ref new" is in C++/CX but that's available only in Windows Store apps. Windows Store apps aren't console apps and don't have the System namespace so that example wouldn't work anyway.

    Monday, March 10, 2014 11:21 AM
    Moderator
  • Hi Mike and Dave

    I have seen a few people using "ref class" in this website so I am curious. I put "Kim::Name^ p=gcnew Kim::Name("Smith");" but it gets me a compilation error:

    1>ConsoleApplication2.cpp(35): error C2065: 'ref' : undeclared identifier

    1>ConsoleApplication2.cpp(35): error C2147: syntax error : 'gcnew' is a new keyword

    How can I make it work?

    Regards

    Chong

    Monday, March 10, 2014 11:46 AM
  • Hmm, I bet that you used "ref gcnew". It's just gcnew, without any ref in front of it.
    Monday, March 10, 2014 1:23 PM
    Moderator
  • Hi Mike

    Could you please tell me what "ref class" does in your example program?

    Regards

    Chong

    Monday, March 10, 2014 2:56 PM
  • "Could you please tell me what "ref class" does in your example program?"

    I already said that in my first reply. I'm not sure what else I can add that's not simply a copy of the existing documentation.

    Monday, March 10, 2014 3:15 PM
    Moderator
  • Hi Mike

    According to the example in http://msdn.microsoft.com/en-us/library/windows/apps/hh699870.aspx, "Kim::Name^ p=ref new Kim::Name("Smith");" should be alright?

    Regards

    Chong

    Monday, March 10, 2014 3:36 PM
  • Hi Mike

    Could you please tell me what "ref class" does in your example program?

    Regards

    Chong

    (Sorry, I missed that you had used 'ref new' in your previous post.)

    A 'ref class' is a different kind of class from a regular C++ class. Because it is different, it has to have a different notation. In C++/CLI there is also 'value class'.

    Unless you have a specific reason to learn it, I would forget about C++/CLI for now.


    David Wilkinson | Visual C++ MVP

    Monday, March 10, 2014 3:42 PM
  • There are 2 different language extensions:

    1. C++/CLI - this targets the .NET runtime
    2. C++/CX - this targets the WinRT runtime

    There are a lot of syntax similarities between those 2 extensions but there are also a few differences. Probably the 'ref new'/'gcnew' is the biggest difference when it comes to syntax, 'ref new' is for C++/CX and 'gcnew' is for C++/CLI. The example I have posted is for C++/CLI so you have to use 'gcnew', not 'ref new'.

    I've posted a C++/CLI example because it's easier to play with it. You simply create a CLR Console Application, paste the code in it and run it.

    To play with C++/CX you need to create a Windows Store application. That's easy to do since the wizard generates all the necessary startup code. But these applications are basically GUI applications and in order to be able do something meaningful you also have to learn a bit of XAML.

    Monday, March 10, 2014 3:54 PM
    Moderator