Shadow Blade
Morrowland Presents Apron Tutorials
game - design - multimedia - web - programming - tutorials - There are currently viewers visiting Apron Tutorials.
   
 
TECHNICAL
GRAPHICAL
SOUND
GAME
WEB
ARTICLE
TOOL
PROJECT
 
 NEWS

  news

  history


 TECHNICAL TUTORIALS

  opengl

  direct3d

  c/c++

  visual basic

  java

  c#


 WEB TUTORIALS

  html

  dhtml

  asp

  php


 IMAGE EDITING

  photoshop

  draw sketch


 3D MODELING

  3d studio


 PROJECTS

  shadow blade

  game environment

  virtual 3d guide

  billy blue flame


 DEMOS

  win32 api


 ARTICLES

  general

  opengl


 COMMUNITY

  about us

  credit

  contact












#include <stdio.h>
#include <iostream>	
using namespace std;					

// declare our swap number function
void swap_numbers(int &num1, int &num2);


int main()
{	

	// PART I  ////////////////////

	// declare a new variable num as integer
	int num;
	// let Ref be a reference to num (create an other name for num)
	int &Ref = num;
	// since Ref and num are basicly the same variable (same address),
	// they both get the same value
	Ref = 8; 

	cout << "num  = " << num << endl << endl; // display num



	// PART II  ///////////////////

	int num1 = 1; // num1 = 1
	int num2 = 2; // num2 = 3

	swap_numbers(num1, num2); // swap the numbers 

	 // display numbers
	cout << "num1 = " << num1 << endl << "num2 = " << num2;


	getchar(); // wait for a key to be pressed

	return 0; // return zero
}



// define our swap number function
void swap_numbers(int &num1, int &num2)
{ 
	int temp = num1;
	num1 = num2;
	num2 = temp;
}

/*

This tutorial demonsterates the use of references.
The ability to use a reference is one of the things that separates c++
from the other programming languages. A Reference can be defined as an extra
name for an already existing variable. When you see this symbol & in c++ it
means that a reference is used. 


When we declare a variable it's really just a series of memory cells,
that we can acess by an identifier.
example: a short int uses 16 bit (2 byte), a long int uses 32 bit (4 byte)

Let say that when you declare int myVariable; it uses 32 bit,
and the variable gets an unique address in memory.

A good simile for the computer memory can be a street in a city.
On a street all houses are consecutively numbered with an unique
identifier so if we talk about 27th of Sesame Street we will be
able to find that place without loss, since there must be only one
house with that number and, in addition, we know that that house 
will be between houses 26 and 28. 

In the same way in which houses in a street are numbered,
the operating system organizes the memory with unique and
consecutive numbers, so if we talk about location 1776 in the
memory, we know that there is only one location with that address
and also that is between addresses 1775 and 1777. 


At the moment in which we declare a variable this one must be stored
in a concrete location/address in this succession of cells (the memory).
We generally do not decide where the variable is to be placed - fortunately
that is something automatically done by the compiler and the 
operating system on runtime, but once the operating system has
assigned an address there are some cases in which we may be
interested in knowing where the variable is stored. 

This can be done by preceding the variable identifier by an
ampersand sign (&), which literally means "address of". 

When we use &num1 we refer to the address of num1.
So when we pass in the reference of the integer num1 and num2,
and use our swap function. We swap the actual numbers
of num1 and num2 evan thoug they are declared in an other function.

This is also called an out-argument. Functions can only return one value,
but they can have many out-arguments.

A referance can not be declared without a variable to refer to.

*/



Download The Source Code! Download Visual C++ Source Code


I hope you found this c++ tutorial useful!

Don't forget to mention Apron Tutorials in your References!

Best Regards
Ronny André Reierstad

 LINKS

  morrowland home



 GAME DEVELOPEMENT

  nvidia

  gamedev

  polycount


 PROJECTS

  shadow blade

  game environment

  virtual 3d guide

  billy blue flame


 MATH AND PHYSICS

  mathworld

  physicsworld







PLEASE LINK TO


  All rights reserved Morrowland © 2011  
Ronny André Reierstad