12.12.2012 Views

Teach Yourself Borland C++ in 14 Days - portal

Teach Yourself Borland C++ in 14 Days - portal

Teach Yourself Borland C++ in 14 Days - portal

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Up to Your Neck <strong>in</strong> <strong>C++</strong><br />

Although it might appear that references are preferred over po<strong>in</strong>ters, that is not the case.<br />

References have some peculiarities that make them unsuitable <strong>in</strong> many cases. For one th<strong>in</strong>g,<br />

references cannot be declared and then later assigned a value. They must be <strong>in</strong>itialized when<br />

declared. For <strong>in</strong>stance, the follow<strong>in</strong>g code snippet will result <strong>in</strong> a compiler error:<br />

MyStruct* pStruct = new MyStruct;<br />

MyStruct& ref;<br />

ref = *pStruct;<br />

ref.X = 100;<br />

Another problem with references is that they cannot be set to 0 or NULL as po<strong>in</strong>ters can. That<br />

means you’ll have to take special care to ensure that a reference is not deleted twice. References<br />

and po<strong>in</strong>ters can often serve the same purpose, but neither is perfect <strong>in</strong> every programm<strong>in</strong>g<br />

situation.<br />

Pass<strong>in</strong>g Function Parameters by<br />

Reference and by Po<strong>in</strong>ter<br />

Earlier I talked about pass<strong>in</strong>g objects to functions by value. I said that <strong>in</strong> the case of structures<br />

and classes, it is usually better to pass those objects by reference rather than by value. Any<br />

object can be passed by reference. This <strong>in</strong>cludes the primitive data types such as <strong>in</strong>t and long,<br />

as well as <strong>in</strong>stances of a structure or class. To review, when you pass function parameters by<br />

value, a copy of the object is made, and the function works with the copy. When you pass<br />

by reference, a po<strong>in</strong>ter to the object is passed and not the object itself. This has two primary<br />

implications. First, it means that objects passed by reference can by modified by the function.<br />

Second, pass<strong>in</strong>g by reference elim<strong>in</strong>ates the overhead of creat<strong>in</strong>g a copy of the object.<br />

The fact that an object can be modified by the function is the most important aspect of<br />

pass<strong>in</strong>g by reference. Take this code, for <strong>in</strong>stance:<br />

void IncrementPosition(<strong>in</strong>t& xPos, <strong>in</strong>t& yPos)<br />

{<br />

xPos++;<br />

yPos++;<br />

}<br />

<strong>in</strong>t x = 20;<br />

<strong>in</strong>t y = 40;<br />

IncrementPosition(x, y);<br />

// x now equals 21 and y equals 41<br />

Notice that when the function returns, both of the parameters passed have been <strong>in</strong>cremented<br />

by one. This is because the function is modify<strong>in</strong>g the actual object via the po<strong>in</strong>ter (remember<br />

that a reference is a type of po<strong>in</strong>ter).<br />

79<br />

3

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!