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.

492 Day <strong>14</strong><br />

__classid<br />

The __classid keyword generates a po<strong>in</strong>ter to a class’s virtual function table (vtable).<br />

__classid is for <strong>C++</strong>Builder’s <strong>in</strong>ternal use and it is not recommended that you use this<br />

keyword <strong>in</strong> your applications.<br />

__closure<br />

A closure is a special type of function po<strong>in</strong>ter. A regular function po<strong>in</strong>ter is a 4-byte value that<br />

po<strong>in</strong>ts to a function with<strong>in</strong> the program’s code segment. A closure, on the other hand, is an<br />

8-byte value that conta<strong>in</strong>s not only the function address (<strong>in</strong> the first 4 bytes), but also the<br />

address of the specific object <strong>in</strong> which the function resides (<strong>in</strong> the last 4 bytes). This type of<br />

function po<strong>in</strong>ter is vital to the way <strong>C++</strong>Builder programs operate. In Pascal this is termed a<br />

method po<strong>in</strong>ter. A closure is the mechanism that allows you, at design time or runtime, to<br />

assign an event handler <strong>in</strong> one class (<strong>in</strong> your ma<strong>in</strong> form’s class, for <strong>in</strong>stance) with an event<br />

that occurs <strong>in</strong> another class (such as <strong>in</strong> a menu class or a button class). While this is very<br />

common <strong>in</strong> <strong>C++</strong>Builder programm<strong>in</strong>g, it might come as a surprise to learn that the <strong>C++</strong><br />

language does not <strong>in</strong>herently have this ability. The __closure keyword extends <strong>C++</strong> to allow<br />

events and event handlers to work as they do.<br />

A closure, then, is used with events. An event is a property that uses a function po<strong>in</strong>ter as its<br />

data type. So an event is a closure. You can declare a closure just as you do a regular function<br />

po<strong>in</strong>ter, only with the addition of the __closure keyword:<br />

void __fastcall (__closure *TMyEvent)(System::TObject *Sender);<br />

You will generally only use the __closure keyword when writ<strong>in</strong>g events for components.<br />

While your day-to-day use of <strong>C++</strong>Builder will rarely make direct use of the __closure<br />

keyword, you will still use closures heavily whether you realize it or not. Closures are<br />

someth<strong>in</strong>g that should, <strong>in</strong> my op<strong>in</strong>ion, be <strong>in</strong> the <strong>C++</strong> language standard.<br />

__declspec<br />

The __declspec keyword is not new to <strong>Borland</strong> <strong>C++</strong> compilers, but it has been extended for<br />

use <strong>in</strong> <strong>C++</strong>Builder. The delphiclass and delphireturn arguments are new to <strong>C++</strong>Builder.<br />

You use __delcspec(delphiclass) any time you forward declare a class derived from TObject<br />

(whether derived directly or <strong>in</strong>directly). You use __delcspec(delphireturn) any time you make<br />

a forward declaration of a class derived from Currency, AnsiStr<strong>in</strong>g, Variant, TDateTime, or Set.

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

Saved successfully!

Ooh no, something went wrong!