Home

Data Types

   

Data Types

 

Variable Initialization

When you declare a variable, as mentioned already, the compiler reserves a space in memory for that variable in the stack. In most languages, that space is left empty until you put a value in it. Initialization is a technique of putting a value into the space memory of a variable. We also state that variable is assigned a value.

 

To control the behavior of your program, you can assign the desired value to a variable when you declare it. That is, you can initialize a variable when declaring it. There are two main techniques used to initialize a variable.

You can use the equal symbol (also called the assignment operator) to initialize a variable. To do that, after typing the name of the variable, type = followed by the desired value. The formula used would be:

TypeOfVariable VariableName = InitialValue;

Another technique of initializing a variable is by using parentheses. The syntax is:

TypeOfVariable VariableName(InitialValue);

Introduction to Data Types

We have mentioned that the syntax of declaring a variable was:

TypeOfVariable VariableName;

The TypeOfVariable factor lets the compiler know the amount of memory that will be needed for the variable. This TypeOfVariable is commonly called the data type. As we will find out, there are various data types available in C++.

If many different variables are using the same data type, you can declare them on the same line, separating two with a comma, except the last one that would end with a semi-colon. The formula to follow is:

DataType Variable1, Variable2, Variable3;
 

Practical LearningPractical Learning: Introducing Data Types Applications

  1. To start a new program, launch Microsoft Visual C++ 2005
  2. On the main menu, click File -> New -> Project...
  3. On the left side, make sure that Visual C++ is selected. In the Templates list, click CLR Empty Project
  4. In the Name box, replace the string with RealEstate2 and click OK
  5. To create a source file, on the main menu, click Project -> Add New Item...
  6. In the Templates list, click Source File (.cpp)
  7. In the New box, type Exercise and click Add
  8. In the empty file, type:
     
    using namespace System;
    
    int main()
    {
        Console::WriteLine("=//= Altair Realty =//=");
        Console::WriteLine("-=- Properties Inventory -=-");
        return 0;
    }
  9. To execute the application, on the main menu, click Debug -> Start Without Debugging
  10. Click Yes

Integers

 

Signed and Unsigned Numbers

An integer is a natural number typically used to count items. An integer is considered a "whole number" because it doesn't display a decimal fraction.

A number can be expressed as being positive or negative. When a number is written like 248, it is considered positive. Such a number is greater than 0. To specify that a number is negative, you must type - to its left. An example would be -248. When declaring a variable that would hold a number, you will have the option of specifying that the variable can hold a positive or a negative value.

A variable is referred to as signed when it can hold either a positive or a negative number. The positive sign can be set by either not typing any sign to its left, as in 248, or by adding + to its left, as in +248.

A variable is referred to as unsigned if it must hold only a positive number. If it is given a negative number, in the best case scenario, the compiler would not do anything. In the bad case scenario, the value would be unpredictable. With some compilers, the program would crash.

Bytes

The most fundamental number in C++/CLI is represented by the Byte data type (this data type is not formally used in C++). A Byte is an unsigned number whose value ranges from 0 to 255. To declare such a variable, you can use the Byte keyword. The Byte data type is defined in the System namespace. Here is an example:

using namespace System;

int main()
{
	Byte Age = 242;

	Console::WriteLine(Age);

	return 0;
}

This would produce:

242
Press any key to continue

If you want to use a small number but whose variable can hold either positive or negative values, you can declare it using the SByte data type. A variable declared with SByte can hold a value between -127 and 128, not more, not less. The SByte data type is defined in the System namespace. Here is an example:

using namespace System;

int main()
{
	SByte Age = 24;

	Console::WriteLine(Age);

	return 0;
}

This would produce:

24

Press any key to continue
 

Practical LearningPractical Learning: Declaring a Byte Variable

  1. To declare a Byte variable, change the file as follows:
     
    using namespace System;
    
    int main()
    {
        Byte stories = 2;
    
        Console::WriteLine("=//=  Altair Realty  =//=");
        Console::WriteLine("-=- Properties Inventory -=-");
        Console::WriteLine("Property:");
        Console::Write("Stories: ");
        Console::WriteLine(stories);
        
        return 0;
    }
  2. Execute the project to see the result:
     
    =//=  Altair Realty  =//=
    -=- Properties Inventory -=-
    Property:
    Stories: 2
    Press any key to continue . . .
  3. Close the DOS window

Short Variables

If you need to represent a number that is a little higher than the Byte can hold, you can declare it using the short keyword. Here is an example:

using namespace System;

int main()
{
	short Pages = 424;

	Console::WriteLine(Pages);

	return 0;
}

A short variable can hold a natural number whose value ranges from -32768 to 32767. Based on this, it is important to note that a Byte value can fit in a short. Therefore, it is normal to declare as short a variable that would hold even small numbers such as people's ages.

When assigning a value to the variable, you can use a number in that range. If you assign a value out of that range, you would receive a warning and the number would be "truncated". That is, the number would be converted to the value of the other extreme. Consider the following program:

using namespace System;

int main()
{
	short Number = 32769;

	Console::WriteLine(Number );

	Console::WriteLine();
	return 0;
}

This would produce the following warning:

warning C4309: 'initializing' : truncation of constant value

The program would produce:

-32767

Press any key to continue

If you want to enforce the idea that the variable is signed, you can type the signed keyword before short. Here is an example:

using namespace System;

int main()
{
	signed short Number = -326;

	Console::WriteLine(Number );

	Console::WriteLine();
	return 0;
}

If you want the variable to hold only positive numbers, you can type the unsigned keyword to the left of short and make sure you omit the signed keyword. When using the unsigned keyword, you can store numbers that range from 0 to 65535. If you assign either a negative number or a number higher than 65535, the number would be truncated. This time, the truncation is done differently but the result is not as intended. Here is an example:

using namespace System;

int main()
{
	unsigned short Number = -326;

	Console::WriteLine(Number );

	return 0;
}

After a warning, this would produce:

65210
Press any key to continue

Besides the short keyword, to declare a variable for a relatively small number, you can use the Int16 data type. The Int16 data type is defined in the System namespace. Its variable can hold values in the same range as the short. Here is an example:

using namespace System;

int main()
{
	System::Int16 Number = -326;

	Console::WriteLine(Number );

	Console::WriteLine();
	return 0;
}

Because an Int16 variable can hold negative values, if you want to use only positive values, you can declare the variable using the UInt16 data type (The U stands for "unsigned"). The UInt16 data type is defined in the System namespace. Here is an example:

using namespace System;

int main()
{
	System::UInt16 Number = 60326;

	Console::WriteLine(Number );

	Console::WriteLine();
	return 0;
}

This would produce:

60326

Press any key to continue

Integral Variables

In some cases you may need a variable that hold a number larger than the short or the Int16 can carry. To declare such a variable, you can use the int keyword. The int data type is used for a variable whose value can range from –2,147,483,648 to 2,147,484,647. Here is an example:

using namespace System;

int main()
{
	int Number = 602;

	Console::WriteLine(Number);

	Console::WriteLine();
	return 0;
}

Compiled, the program would produce:

602

Press any key to continue

It is important to note that, based on its range of values, an int variable can hold the same values as the Byte, the short, or the Int16. Therefore, it is perfectly normal and sometimes preferable to use int for a variable intended to hold natural numbers.

By default, an int declared variable can hold either positive or negative values. You can still enforced this by typing the signed keyword to the left of int:

using namespace System;

int main()
{
	signed int Number = 602;

	Console::WriteLine(Number);

	Console::WriteLine();
	return 0;
}

If you want the variable to hold only positive numbers, you can type the unsigned keyword to its left and omit the signed. Here is an example:

using namespace System;

int main()
{
	unsigned int Number = 46082;

	Console::WriteLine(Number);

	return 0;
}

Besides the int keyword, you can use the Int32 data type. An Int32 holds the same range of values as the int. The Int32 data type is defined in the System namespace. Here is an example:

using namespace System;

int main()
{
	System::Int32 Number = 46802;

	Console::WriteLine(Number);

	Console::WriteLine();
	return 0;
}

To specify that the variable must hold only positive numbers, besides unsigned int, you can use the UInt32 data type to declare the variable. The UInt32 data type is defined in the System namespace. Here is an example:

using namespace System;

int main()
{
	UInt32 Number = 46082;

	Console::WriteLine(Number);

	return 0;
}
 

Practical LearningPractical Learning: Declaring Unsigned Variables

  1. To use unsigned variables, change the file as follows:
     
    using namespace System;
    
    int main()
    {
        Byte stories = 2;
        unsigned bedrooms = 5;
        unsigned yearBuilt = 1962;
    
        Console::WriteLine("=//=  Altair Realty  =//=");
        Console::WriteLine("-=- Properties Inventory -=-");
        Console::WriteLine("Property:");
        Console::Write("Stories:    ");
        Console::WriteLine(stories);
        Console::Write("Bedrooms:   ");
        Console::WriteLine(bedrooms);
        Console::Write("Year Built: ");
        Console::WriteLine(yearBuilt);
        
        return 0;
    }
  2. Execute the project to see the result:
     
    =//=  Altair Realty  =//=
    -=- Properties Inventory -=-
    Property:
    Stories:  2
    Bedrooms: 5
  3. Close the DOS window

Long Integers

While the int and the Int32 data types can hold significantly large numbers, you may still need a variable that can hold very large numbers. Such a variable can be declared using the long data type. Such a variable can hold a number that ranges from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. Based on this, it is important to note that a long variable can hold a short, an int, an Int16 or an Int32 variable but, unlike the int and the Int32 that are more commonly used, the long is more appropriate if you know that the variable will really need to carry very large numbers. Otherwise, the long can use significant memory.

Here is an example:

using namespace System;

int main()
{
	long Number = 46082;

	Console::WriteLine(Number);

	return 0;
}

This would produce:

46082
Press any key to continue

When declaring a variable as long, it can hold either positive or negative numbers. You can still enforce this by declaring the variable as signed long.

 

Practical LearningPractical Learning: Declaring Long Integers

  1. To declare a long variable, change the file as follows:
     
    using namespace System;
    
    int main()
    {
        long propertyNumber = 490724;
        Byte stories = 2;
        unsigned bedrooms = 5;
        unsigned yearBuilt = 1962;
    
        Console::WriteLine("=//=  Altair Realty  =//=");
        Console::WriteLine("-=- Properties Inventory -=-");
        Console::Write("Property #: ");
        Console::WriteLine(propertyNumber);
        Console::Write("Stories:    ");
        Console::WriteLine(stories);
        Console::Write("Bedrooms:   ");
        Console::WriteLine(bedrooms);
        Console::Write("Year Built: ");
        Console::WriteLine(yearBuilt);
        
        return 0;
    }
  2. Execute the project to see the result:
     
    =//=  Altair Realty  =//=
    -=- Properties Inventory -=-
    Property #: 490724
    Stories:    2
    Bedrooms:   5
    Year Built: 1962
    Press any key to continue . . .
  3. Close the DOS window

Initializing an Integral Variable

After declaring an integer variable, you can initialize it with an appropriate value. As far as the value is concerned, you have two main alternatives. You can initialize an integral variable with a decimal value as we have done so far. Alternatively, you can initialize an integral variable with a hexadecimal value.

 

 

 

 

 
 

Characters

 

Symbolic Characters

A character is a symbol that displays on your screen. It can be a letter such as a, b, c, d, e, f, g, h, I, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, and z. It can also be a letter from A to Z. It can also be a digit such as 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9. It can also by a special characters such as ` ~ # $ ! @ % ^ & * ( { [ ) } ] | \ : ; “ ‘ + - < _ ? > , / =. A character is really an integer whose value can range from -128 to 127.

To declare a character variable, you use the char keyword. Here is an example:

using namespace System;

int main()
{
	char AlphaLetter;

	Console::WriteLine();
	return 0;
}

Since a char variable represents one symbol, to initialize it, enclose the initial value in single quotes. Here are examples:

using namespace System;

int main()
{
	char AlphaLetter = 'd';
	char Pick('G');

	return 0;
}

A character variable can also be declared with the signed char data type. This type of variable would be an integer whose value can range from –128 to +127.

Unicode Characters

One of the limitations of the char data type is that it can fit only on a single character (limited to 8 bits). To support a wider range of characters, you can use the Char or the __wchar_t data types to declare a character variable. In fact, you should use either Char or __wchar_t whenever you need a character. The Char and the __wchar_t data types are defined in the System namespace. Here is an example:

using namespace System;

int main()
{
	__wchar_t AlphaLetter = 'F';

	Console::WriteLine(AlphaLetter);

	return 0;
}

This would produce:

F
Press any key to continue

Based on their structures, Char and __wchar_t variable can accommodate letters in many other languages than Latin-based. When initializing a variable declared with Char or __wchar_t, to indicate that you are using a Unicode character, you can precede the quoted value with L. Here is an example:

using namespace System;

int main()
{
	Char Letter = L'D';

	Console::WriteLine(Letter);

	return 0;
}

This would produce:

D
Press any key to continue
 

Practical LearningPractical Learning: Using Character Variables

  1. To use a character variable, change the file as follows:
     
    using namespace System;
    
    int main()
    {
        long propertyNumber = 490724;
        __wchar_t propertyType = 'S';
        Byte stories = 2;
        unsigned bedrooms = 5;
        unsigned yearBuilt = 1962;
    
        Console::WriteLine("=//=  Altair Realty  =//=");
        Console::WriteLine("-=- Properties Inventory -=-");
        Console::Write("Property #:    ");
        Console::WriteLine(propertyNumber);
        Console::Write("Property Type:  ");
        Console::WriteLine(propertyType);
        Console::Write("Stories:        ");
        Console::WriteLine(stories);
        Console::Write("Bedrooms:       ");
        Console::WriteLine(bedrooms);
        Console::Write("Year Built:     ");
        Console::WriteLine(yearBuilt);
        
        return 0;
    }
  2. Execute the project to see the result:
     
    =//=  Altair Realty  =//=
    -=- Properties Inventory -=-
    Property #:    490724
    Property Type:  S
    Stories:        2
    Bedrooms:       5
    Year Built:     1962
    Press any key to continue . . .
  3. Close the DOS window

The String: A List of Characters

A string is a group of characters considered as one unit, as opposed to a single character as we introduced above. Unlike some other languages such as (Object) Pascal or (Visual) Basic, C++ doesn't have a data type that can hold a group of characters as one entity. The alternative was to "create" and adapt a string data type in additional library. This problem was partially solved in a library called Standard Template Library (STL). Therefore, the STL provides the string data type.

To declare a variable that can hold a string, you can use the string data type (the string is a class but we will consider it a data type for simplicity). The value of the string must be included in double-quotes. Here is an example:

string Course = "Business Mathematics";

The string data type is part of the std namespace and it is defined in the string library. When using the string data type, make sure you include the string library and use the std namespace. To display the value of a string variable, you can use the cout extractor. Here is an example:

#include <string>
#include <iostream>
using namespace std;
using namespace System;

int main()
{
	string Course = "Business Mathematics";

	cout << Course;

	Console::WriteLine();
	return 0;
}

This would produce:

Business Mathematics
Press any key to continue

Decimal Numbers

 

Floating-Point Variables

The integers we have used so far don't allow decimal values. C++ provides floating identifier values that would solve this problem. To declare a variable that involves a decimal value, you can use the float keyword. Here is an example:

using namespace System;

int main()
{
	float Fraction = 12.35;

	Console::WriteLine(Fraction);

	Console::WriteLine();
	return 0;
}

A float variable can hold a number that ranges from 3.4 x 10-38 to 3.4 x 1038. Besides, or instead of, float, you can use the Single data type to declare a variable that holds a simple decimal value. The Single data type is defined in the System namespace.

 

Practical LearningPractical Learning: Using a Single-Precision Variables

  1. To declare a single-precision variable, change the file as follows:
     
    using namespace System;
    
    int main()
    {
        long propertyNumber = 490724;
        __wchar_t propertyType = 'S';
        Byte stories = 2;
        unsigned bedrooms = 5;
        float bathrooms = 3.5;
        unsigned yearBuilt = 1962;
    
        Console::WriteLine("=//=  Altair Realty  =//=");
        Console::WriteLine("-=- Properties Inventory -=-");
        Console::Write("Property #:    ");
        Console::WriteLine(propertyNumber);
        Console::Write("Property Type:  ");
        Console::WriteLine(propertyType);
        Console::Write("Stories:        ");
        Console::WriteLine(stories);
        Console::Write("Bedrooms:       ");
        Console::WriteLine(bedrooms);
        Console::Write("Bathrooms:      ");
        Console::WriteLine(bathrooms);
        Console::Write("Year Built:     ");
        Console::WriteLine(yearBuilt);
        
        return 0;
    }
  2. Execute the project to see the result:
     
    =//=  Altair Realty  =//=
    -=- Properties Inventory -=-
    Property #:    490724
    Property Type:  S
    Stories:        2
    Bedrooms:       5
    Bathrooms:      3.5
    Year Built:     1962
    Press any key to continue . . .
  3. Close the DOS window

Double-Precision Variables

One of the limitations of the float data type is that it provides less precision than required sometimes (in fact, the Microsoft C++ compiler, always displays a warning when you use the float data type to declare a variable). The alternative is to use the double keyword or the Double data type to declare a variable that would hold decimal numbers. The Double data type is defined in the System namespace.

A double-precision variable declared with double or Double can hold a decimal or fractional number that ranges from 1.7 x 10-308 to 1.7 x 10308. It is used for numbers that and/or are very large.

using namespace System;

int main()
{
	System::Double Fraction = 12.35;

	Console::WriteLine(Fraction);

	return 0;
}

This would produce:

12.35
Press any key to continue
 

Practical LearningPractical Learning: Using a Single-Precision Variables

  1. To use a double-precision variable, change the file as follows:
     
    using namespace System;
    
    int main()
    {
        long propertyNumber = 490724;
        __wchar_t propertyType = 'S';
        Byte stories = 2;
        unsigned bedrooms = 5;
        float bathrooms = 3.5;
        unsigned yearBuilt = 1962;
        double marketValue = 652540;
    
        Console::WriteLine("=//=  Altair Realty  =//=");
        Console::WriteLine("-=- Properties Inventory -=-");
        Console::Write("Property #:    ");
        Console::WriteLine(propertyNumber);
        Console::Write("Property Type:  ");
        Console::WriteLine(propertyType);
        Console::Write("Stories:        ");
        Console::WriteLine(stories);
        Console::Write("Bedrooms:       ");
        Console::WriteLine(bedrooms);
        Console::Write("Bathrooms:      ");
        Console::WriteLine(bathrooms);
        Console::Write("Year Built:     ");
        Console::WriteLine(yearBuilt);
        Console::Write("Market Value:   ");
        Console::WriteLine(marketValue);
        
        return 0;
    }
  2. Execute the project to see the result:
     
    =//=  Altair Realty  =//=
    -=- Properties Inventory -=-
    Property #:    490724
    Property Type:  S
    Stories:        2
    Bedrooms:       5
    Bathrooms:      3.5
    Year Built:     1962
    Market Value:   652540
    Press any key to continue . . .
  3. Close the DOS window

Initializing a Double-Precision Variable

You may have found out that when you declare and initialize a float or Single variable, the compiler generates a warning. Consider the following example:

using namespace System;

int main()
{
	float Fraction = 12.35;

	Console::WriteLine(Fraction);

	Console::WriteLine();
	return 0;
}

When compiled, you would get the following warning:

.\Exercise.cpp(5) : warning C4305: 'initializing' : truncation from 'double' to 'float'

Although the program would compile fine. By default, when the compiler sees a value such as 12.35, for the sake of precision, it tends to store it as a double value. If you really want the variable to be treated as a float or Single, add the f or F to its right. Here is an example:

using namespace System;

int main()
{
	float Fraction = 12.35f;

	Console::WriteLine(Fraction);

	Console::WriteLine();
	return 0;
}

This time, the compiler will not generate a warning. Remember that you can use f (lowercase) or F (uppercase).

Variable Reference

 

The typedef Type Definition

The data types we have used so far may have names we are not familiar with. C++ allows you to customize the name of a data type to a name you are more familiar with. You would not (and are not allowed to) create a new data type, you would only "redefine" the name of an existing data type.

To customize the name of a data type, you can use the typedef keyword. The formula used is:

typedef KnownDataType NewName;

The typedef keyword is required to let the compiler know that you are creating a new data type. The data type must be one that exists already, such as those we have reviewed so far. It could be an int, an unsigned int, an Int32, a char, a double, etc. An example of using a typedef is:

using namespace System;

int main()
{
	typedef unsigned int PositiveNumber;

	return 0;
}

In this case, PositiveNumber is just a new name for an unsigned int. It can be used as a new data type exactly as if you were using an unsigned int. Here are examples:

using namespace System;

int main()
{
	typedef unsigned int PositiveNumber;
	typedef double	   Salary;

	PositiveNumber Gender   = 1;
	Salary WeeklySalary     = 1450.88;

	Console::Write("Gender: ");
	Console::WriteLine(Gender);
	Console::Write("Salary: ");
	Console::WriteLine(WeeklySalary);

	return 0;
}

This would produce:

Gender: 1
Salary: 1450.88
Press any key to continue . . .

Native References

A reference is a variable that is a duplicate of an existing variable. It provides a technique of creating more than one name to designate the same variable. The syntax of creating or declaring a reference is:

DataType &ReferenceName = VariableName;

To declare a reference, type the variable’s name preceded by the same type as the variable it is referring to. Between the data type and the reference name, type the ampersand operator “&”. To specify what variable the reference is addressed to, use the assignment operator “=” followed by the name of the variable. The referred to variable must exist already. You cannot declare a reference as:

int &Mine;

The compiler wants to know what variable you are referring to. Here is an example:

using namespace System;

int main()
{
	int Number = 228;
    	int &Nbr = Number;
	
	Console::WriteLine(Number);
	Console::WriteLine(Nbr);

	return 0;
}

The ampersand operator between the data type and the reference can assume one of three positions as follows:

int& Nbr;
int & Nbr;
int &Nbr;

As long as the & symbol is between a valid data type and a variable name, the compiler knows that the variable name (in this case Nbr) is a reference.

Once a reference has been initialized, it holds the same value as the variable it is pointing to. You can then display the value of the variable using either of both:

#include <iostream>

using namespace std;
using namespace System;

int main()
{
	int Number = 228;
	int &Nbr = Number;
	
	cout << "Number:        ";
	Console::WriteLine(Number);
	cout << "Its reference: ";
	Console::WriteLine(Nbr);
	
	Console::WriteLine();
	return 0;
}

This would produce:

Number:        228
Its reference: 228

Press any key to continue

If you change the value of the variable, the compiler updates the value of the reference so that both variables would hold the same value. In the same way, you can modify the value of the reference, which would update the value of the referred to variable. To access the reference, do not use the ampersand operator; just the name of the reference is sufficient to the compiler. This is illustrated in the following:

#include <iostream>

using namespace std;
using namespace System;

int main()
{
	int Number = 228;
    	int &Nbr = Number;
	
	cout << "Number:        ";
	Console::WriteLine(Number);
	cout << "Its reference: ";
	Console::WriteLine(Nbr);
	
	Number = -15008;
	
	cout << "\nNumber:        ";
	Console::WriteLine(Number);
	cout << "Its reference: ";
	Console::WriteLine(Nbr);

	Number = 28114;
	
	cout << "\nNumber:        ";
	Console::WriteLine(Number);
	cout << "Its reference: ";
	Console::WriteLine(Nbr);

	Console::WriteLine();
	return 0;
}

This would produce:

Number:        228
Its reference: 228

Number:        -15008
Its reference: -15008

Number:        28114
Its reference: 28114

Press any key to continue

It is very important to remember the relationship that a variable in the stack has with its value: A variable in the stack contains its own value. Whenever you access this variable, it gives you copy of its value and retains the original. Once you have that value, you can do what you want with it and this would not have any impact on the variable itself or its value. For example, if you assign such a value to another variable, that other variable receives its own copy. Let's illustrate with the following program:

#include <iostream>

using namespace std;
using namespace System;

int main()
{
    	// TODO: Please replace the sample code below with your own.
	int Number1 = 255;
	int Number2 = Number1;

	Console::WriteLine(Number1);
	Console::WriteLine(Number2);
	
    	Console::WriteLine();
	return 0;
}

This would produce:

255
255

Press any key to continue

Notice that when the value of the first variable has been assigned to the second variable both variables hold the same value. With another assignment, you can change the value of the second variable and the first would keep its value:

#include <iostream>

using namespace std;
using namespace System;

int main()
{
    	// TODO: Please replace the sample code below with your own.
	int Number1 = 255;
	int Number2 = Number1;

	Console::WriteLine(Number1);
	Console::WriteLine(Number2);
	Console::WriteLine();
	
	Number2 = -48;

	Console::WriteLine(Number1);
	Console::WriteLine(Number2);
    	Console::WriteLine();
	return 0;
}

This would produce:

255
255

255
-48

Press any key to continue

This comparison is important because its sets apart the references as we saw above. This will be even more important with pointers in the next lesson.

A Tracking Reference

If you create a C++ reference as done above, the variable is stored in the stack. When the program ends, the compiler reclaims the memory the variable was using. The C++/CLI language provides another way to declare such a reference. In this case, the CLR garbage collector would be able to reclaim the memory that the variable was using. This type of  variable is called a tracking reference.

To create a tracking reference, use the percent operator "%" instead of the ampersand. Here is an example:

int main()
{
	int % PropertyValue;

	return 0;
}

Like a native reference, in order to use a tracking reference, you must initialize it with a variable that has been declared and initialized appropriately. Here is an example:

using namespace System;

int main()
{
	double Value = 468550;
	double % PropertyValue = Value;

	Console::Write("Property Value = $");
	Console::WriteLine(Value);
	Console::Write("Property Value = $");
	Console::WriteLine(PropertyValue);

	return 0;
}

After creating a tracking reference, if you change the value of the variable, the value of the tracking reference would be changed also. If you update the value of the tracking reference, the value of the variable would be updated also.

 

 
 
   
 

Previous Copyright © 2006-2009 FunctionX, Inc. Next