Click here to Skip to main content
15,868,141 members
Articles / Programming Languages / C++/CLI
Article

Using managed reference types in C++

Rate me:
Please Sign up or sign in to vote.
4.78/5 (6 votes)
15 Oct 2001CPOL4 min read 179.7K   887   30   15
A quick introduction to using .NET managed reference types in C++

Introduction

One of the nice things about .NET are the common base classes. These classes are essentially the API of .NET, and are available from all languages equally. Once you know how to use a String in VB.NET you also know how to use it in C# and C++. Once you have struggled up the learning curve for one language you can then go on to use that knowledge in other .NET languages.

In .NET there are value types and reference types. Value types refer to simple data structures such as int's and enumerations, and are stored on the stack. Reference types are created on the .NET managed heap, which allows the garbage collector to track their lifetime and free instances when they are no longer required.

Think of a reference type as a pointer - though not in the traditional C/C++ sense of the word. The actual location of a variable on the managed heap will change as the garbage collector recovers unused memory and compacts the heap, so in a short time a traditional pointer to a spot on the heap will be invalid. A .NET reference, on the other hand, will always give you access to your values no matter where it has been moved on the heap. A variable of reference type will always either contain a reference to a value of that type, or null. Assigning the value of a reference variable to another variable copies the reference, not the value stored. Be warned!

Value types are stored on the stack and are accessed directly. Once the memory containing that value is freed, the value type instance is destroyed. Hence, references to value types are not allowed. If it were allowed it would be possible to have a reference point to an invalid memory location. A value type will always point to a variable of that type, and cannot be null. Assigning a value type to another variable results in a copy of the value being made.

Creating an instance of a reference type

Value types are easy since they are declared on the stack. It would be insane if you had to call new each time you wanted to create an int or double. Reference types are a little more complicated in that they cannot be created on the stack.

Reference types are created on the .NET managed heap, and so must be created using the overloaded new operator. The new operator for managed types not only creates the object on the managed heap, but also initialises the value of the variable to 0. The value passed back from new will not be a .NET reference, and not pointer in the traditional sense. In the following examples we will concentrate on the String class. You'll use it a lot, and it has some tricks up its sleave.

To create an instance of a reference type you simply declare a pointer of the variables type and create the object using new.

String* s = new String("This is a string");

Attempting to declare a managed object on the stack simply won't work:

String s = "This is a string";  // WILL NOT COMPILE

The String class' constructor contains many different overrides for many different occasions, but does not contain an override for String().

Other ways of declaring a String are as follows:

String* s = new String("This is an ANSI string");
String* s = "This is an ANSI string";
String* s = L"This is a UNICODE string";
String* s = S"This is a .NET string";

ANSI and UNICODE strings should be familiar to you. .NET strings (those prefixed by 'S') are new and offer better performance than standard C++ literal strings. As well as this, all instances of identical string literals actually point to the same string. If s1 and s2 are two String's, then the following code:

s1 = S"This is a .NET string";
s2 = S"This is a .NET string";
if (s1 == s2)
	printf("s1 == s2\n");
else
	printf("s1 != s2\n");

s1 = "This is a C++ literal string";
s2 = "This is a C++ literal string";
if (s1 == s2)
	printf("s1 == s2\n");
else
	printf("s1 != s2\n");

would produce

s1 == s2
s1 != s2

Note that C++ literal strings can be used where ever .NET strings are used, but .NET strings cannot be used where C++ strings are expected.

Note also the use of printf in the above snippet. Just because we are using .NET types and methods doesn't mean we lose our standard non-managed libraries. In managed C++ we get the best of both worlds.

Creating your own managed types

Creating your own managed types is achieved using the new __gc keyword.

__gc class MyClass
{
   public: 
       int ID;
};

You then use this class as you would any other managed class:

MyClass* mc = new MyClass;
mc->ID = 5;

Because mc is a managed type it will be automatically initialised to 0 (ie mc->ID will be set as 0).

Using managed types in non-managed functions

The final point is that when combining managed and unmanaged code you will invariably come across situations where you need to pass a managed pointer to a function expecting an unmanaged (fixed) pointer.

To allow this, a new keyword __pin has been introduced that essentially pins down the managed pointer so that the garbage collector will not move it.

MyClass __pin* pMC = mc;
printf("The pinned value of mc is %d\n", pMC->ID);

History

16 Oct 2001 - updated source files for VS.NET beta 2

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Founder CodeProject
Canada Canada
Chris Maunder is the co-founder of CodeProject and ContentLab.com, and has been a prominent figure in the software development community for nearly 30 years. Hailing from Australia, Chris has a background in Mathematics, Astrophysics, Environmental Engineering and Defence Research. His programming endeavours span everything from FORTRAN on Super Computers, C++/MFC on Windows, through to to high-load .NET web applications and Python AI applications on everything from macOS to a Raspberry Pi. Chris is a full-stack developer who is as comfortable with SQL as he is with CSS.

In the late 1990s, he and his business partner David Cunningham recognized the need for a platform that would facilitate knowledge-sharing among developers, leading to the establishment of CodeProject.com in 1999. Chris's expertise in programming and his passion for fostering a collaborative environment have played a pivotal role in the success of CodeProject.com. Over the years, the website has grown into a vibrant community where programmers worldwide can connect, exchange ideas, and find solutions to coding challenges. Chris is a prolific contributor to the developer community through his articles and tutorials, and his latest passion project, CodeProject.AI.

In addition to his work with CodeProject.com, Chris co-founded ContentLab and DeveloperMedia, two projects focussed on helping companies make their Software Projects a success. Chris's roles included Product Development, Content Creation, Client Satisfaction and Systems Automation.

Comments and Discussions

 
GeneralVery nice...Does it come in pink Pin
Sacha Barber23-Jul-10 21:49
Sacha Barber23-Jul-10 21:49 
GeneralCalling managed C++ functions in unmanaged code Pin
swarnimad26-Sep-06 1:18
swarnimad26-Sep-06 1:18 
GeneralC# interface - MC++ implementation Pin
haranath29-May-03 22:51
haranath29-May-03 22:51 
GeneralHELP!!! Pin
User 3874624-Apr-03 7:05
User 3874624-Apr-03 7:05 
Questionwhere can I get vs.net beta2 now? Pin
31-Jan-02 1:47
suss31-Jan-02 1:47 
GeneralWell written Pin
Ben Bryant4-Apr-01 7:21
Ben Bryant4-Apr-01 7:21 
GeneralDisturbing.. Pin
3-Apr-01 5:04
suss3-Apr-01 5:04 
GeneralRe: Disturbing.. Pin
3-Apr-01 8:23
suss3-Apr-01 8:23 
GeneralRe: Disturbing.. Pin
William E. Kempf3-Apr-01 9:45
William E. Kempf3-Apr-01 9:45 
GeneralRe: Disturbing.. Pin
Chris Maunder3-Apr-01 15:44
cofounderChris Maunder3-Apr-01 15:44 
GeneralATL7 is out? Pin
Gerald Schwab3-Apr-01 17:15
Gerald Schwab3-Apr-01 17:15 
Do ou mean the BETA 1 or a release?
GeneralRe: ATL7 is out? Pin
Chris Maunder4-Apr-01 13:20
cofounderChris Maunder4-Apr-01 13:20 
GeneralRe: Disturbing.. Pin
haranath27-May-03 4:35
haranath27-May-03 4:35 
GeneralRe: Disturbing.. Pin
Anonymous27-Apr-05 16:20
Anonymous27-Apr-05 16:20 
GeneralRe: Disturbing.. Pin
Chris Maunder27-Apr-05 17:09
cofounderChris Maunder27-Apr-05 17:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.