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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

76 Day 3<br />

First, on l<strong>in</strong>e 13 I declared the listArray array as an array of po<strong>in</strong>ters. Follow<strong>in</strong>g that,<br />

ANALYSIS<br />

I created objects for each element of the array. This takes place <strong>in</strong> the for loop on<br />

l<strong>in</strong>es 17 and 18. After that, I changed the direct membership operators (.) to <strong>in</strong>direct<br />

membership operators (->). I also have to dereference the po<strong>in</strong>ters on l<strong>in</strong>e 57 and aga<strong>in</strong> on<br />

l<strong>in</strong>e 72. This is necessary because an object is expected and we cannot use a po<strong>in</strong>ter <strong>in</strong> place<br />

of an object. Notice that the displayRecord function (start<strong>in</strong>g on l<strong>in</strong>e 82) doesn’t change. I<br />

haven’t changed the fact that the mail<strong>in</strong>gListRecord structure is passed to the function by<br />

value, so the code <strong>in</strong> the function doesn’t need to be modified.<br />

If you’ve had previous experience with <strong>C++</strong>, you may have noticed that this program has a<br />

bug <strong>in</strong> it. I’ll let you <strong>in</strong> on the secret before the end of the chapter.<br />

References<br />

NEW TERM<br />

A reference is a special type of po<strong>in</strong>ter that allows you to treat a po<strong>in</strong>ter like a regular<br />

object.<br />

References, like po<strong>in</strong>ters, can be confus<strong>in</strong>g. A reference is declared us<strong>in</strong>g the reference operator.<br />

The symbol for the reference operator is the ampersand (&) which is the same symbol used<br />

for the address-of operator (don’t worry, the compiler knows how to keep it all straight). As<br />

I said, a reference allows you to treat a po<strong>in</strong>ter like an object. Here’s an example:<br />

MyStruct* pStruct = new MyStruct;<br />

MyStruct& ref = *pStruct;<br />

ref.X = 100;<br />

Notice that with references you use the direct member operator rather than the <strong>in</strong>direct<br />

member operator as you do with po<strong>in</strong>ters. Now you can get rid of all of those pesky -><br />

operators! Although you won’t use references a lot, they can be very handy when you need<br />

them. By the way, this code snippet could be condensed a little. Here’s how I would write<br />

it <strong>in</strong> a real program:<br />

MyStruct& ref = *new MyStruct;<br />

ref.X = 100;<br />

Although this might look odd, it does exactly the same th<strong>in</strong>g as the first example. Comb<strong>in</strong><strong>in</strong>g<br />

statements like this is common and avoids unnecessary overhead.<br />

Let’s go once more to the MAILLIST example. This time I’ll modify it by implement<strong>in</strong>g a<br />

reference <strong>in</strong> the do-while loop. Actually, I’ll be modify<strong>in</strong>g the POINTER example found <strong>in</strong><br />

List<strong>in</strong>g 3.1. The new program, found <strong>in</strong> List<strong>in</strong>g 3.2, illustrates this change.

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

Saved successfully!

Ooh no, something went wrong!