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.

108 Day 4<br />

“But,” you ask, “what does this mean?” Remember that each class <strong>in</strong>stance gets its own copy<br />

of the class’s data members. But all class <strong>in</strong>stances share the same set of functions for the class<br />

(there’s no po<strong>in</strong>t <strong>in</strong> duplicat<strong>in</strong>g that code for each <strong>in</strong>stance of the class). How does the<br />

compiler figure out which <strong>in</strong>stance goes with which function call? Each class member<br />

function has a hidden this parameter that goes with it. To illustrate, let’s say you have a<br />

function for the Rect class called GetWidth(). It would look like this (no pun <strong>in</strong>tended):<br />

<strong>in</strong>t Rect::GetWidth()<br />

{<br />

return right - left;<br />

}<br />

That’s how the function looks to you and me. To the compiler, though, it looks someth<strong>in</strong>g<br />

like this:<br />

<strong>in</strong>t Rect::GetWidth(Rect* this)<br />

{<br />

return this->right - this->left;<br />

}<br />

That’s not exactly accurate from a technical perspective, but it’s close enough for this<br />

discussion. From this code you can see that this is work<strong>in</strong>g beh<strong>in</strong>d the scenes to keep<br />

everyth<strong>in</strong>g straight for you. You don’t have to worry about how that happens, but only that<br />

it does happen.<br />

WARNING<br />

Never modify the this po<strong>in</strong>ter. You can use it to pass a po<strong>in</strong>ter to your<br />

class to other functions, or as a parameter <strong>in</strong> construct<strong>in</strong>g other classes,<br />

but don’t change its value. Learn to treat this as a read-only variable.<br />

Although this works beh<strong>in</strong>d the scenes, it is still a variable that you can access from with<strong>in</strong><br />

the class. As an illustration, let’s take a quick peek <strong>in</strong>to VCL. Most of the time you will create<br />

components <strong>in</strong> VCL by dropp<strong>in</strong>g them on the form at design time. When you do that,<br />

<strong>C++</strong>Builder creates a po<strong>in</strong>ter to the component and does all sorts of housekeep<strong>in</strong>g chores on<br />

your behalf, sav<strong>in</strong>g you from concern<strong>in</strong>g yourself with the technical end of th<strong>in</strong>gs.<br />

Sometimes, however, you will create a component at runtime. VCL has this funny <strong>in</strong>sistence<br />

(as all good frameworks do) on want<strong>in</strong>g to keep track of which child objects belong to which<br />

parent. For <strong>in</strong>stance, let’s say you wanted to create a button on a form when another button<br />

is clicked. You need to tell VCL who the parent of the new button is. The code would look<br />

like this:<br />

void __fastcall TMyForm::Button1Click(TObject *Sender)<br />

{<br />

TButton* button = new TButton(this);<br />

button->Parent = this;

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

Saved successfully!

Ooh no, something went wrong!