Main Menu


Sponsored Links

  


  
  
Web Cartoon Maker: a Fun Way to Learn C++ Contents Previous Next

Passing Other Types by Reference

It’s not just objects that can be passed by reference. All the other types we’ve seen can, too. For example, to swap two integers, we could write something like:

void Swap ( double &dX, double &dY )

{

double dTemp = dX;

dX = dY;

dY = dTemp;

}

We would call this function in the usual way:

double dX = 0;

double dY = 100;

Max.SetPos ( dX, dY );

Swap ( dX, dY );

Max.GoesTo ( dX, dY, 5 );

When people start passing things like integers by reference, they often try to use an expression as a reference argument. For example:

double dX = 0;

double dY = 100;

Swap ( dX, dY + 1 ); // WRONG!!

This is not legal because the expression dY + 1 is not a variable – it does not occupy a location that the reference can refer to. It is a little tricky to figure out exactly what kinds of expressions can be passed by reference. For now a good rule of thumb is that reference arguments have to be variables.


Contents Previous Next
  
News

New Tales Animator Video by Alan Sturgess

Alan Sturgess shared an excellent video he made using Tales Animator! You can still download Tales Animator here. Unfortunately it is only available for Wi

...

Simple Online Character Designer

There is a prototype of simple online character designer available HERE. It is only a prototype, it does not contain many pieces yet but it can already generat

...

Book is updated

Now our book "Web Cartoon Maker: A Fun Way to Learn C++" is fully in synch with WCM 1.5! It is available for download and online reading HERE.

...

Web Cartoon Maker 1.5 is here!

Web Cartoon Maker 1.5 is finally here! You can download it HERE! Here is what was updated in version 1.5: Web Cartoon Maker Desktop Edition is now fully standal

...

read more news...


Poll