Home

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-2016, FunctionX, Inc. Next