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.

NOTE<br />

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

Now you are free to pass by reference and not worry that your object might be modified by<br />

the function. Note that the function call itself stays the same and that only the function<br />

def<strong>in</strong>ition (and declaration) is modified with the const keyword.<br />

If you attempt to modify a const object, you will get a compiler error<br />

stat<strong>in</strong>g, Cannot modify a const object. The follow<strong>in</strong>g code will<br />

generate that error message:<br />

void someFunction(const MyStruct& s)<br />

{<br />

s.dataMember = 100; // cannot modify a const object<br />

return;<br />

}<br />

Once you declare an object as const, the compiler will make sure you<br />

don’t modify the object.<br />

Note that the object is const only with<strong>in</strong> the function. The object can be modified both<br />

before and after the function returns (provided it was not <strong>in</strong>itially declared as const).<br />

Pass<strong>in</strong>g by po<strong>in</strong>ter is essentially the same as pass<strong>in</strong>g by reference. Pass<strong>in</strong>g by po<strong>in</strong>ter has a<br />

couple of syntactical headaches that make it less desirable than pass<strong>in</strong>g by reference. Let’s take<br />

the IncrementPosition() function from the first example <strong>in</strong> this section and modify it to pass<br />

by po<strong>in</strong>ter rather than by reference:<br />

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

{<br />

*xPos++; // dereference, then <strong>in</strong>crement<br />

*yPos++;<br />

}<br />

Note that the po<strong>in</strong>ter has to be dereferenced before it can be <strong>in</strong>cremented. Most of the time<br />

your needs will be best served by pass<strong>in</strong>g by reference, but you may pass by po<strong>in</strong>ter if a<br />

situation dictates the need. When pass<strong>in</strong>g char arrays, you will usually pass by po<strong>in</strong>ter rather<br />

than by reference because you can use a po<strong>in</strong>ter to a char array and the name of the array<br />

<strong>in</strong>terchangeably. When pass<strong>in</strong>g character arrays, it is better to pass by po<strong>in</strong>ter.<br />

The new and delete Operators<br />

Up to this po<strong>in</strong>t I have been talk<strong>in</strong>g primarily about aspects of the <strong>C++</strong> language that come<br />

from C. From this po<strong>in</strong>t on we’ll be look<strong>in</strong>g at features that are specific to the <strong>C++</strong> language.<br />

The new and delete operators are two important <strong>C++</strong> language features.<br />

81<br />

3

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

Saved successfully!

Ooh no, something went wrong!