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.

154 Day 5<br />

VCL for <strong>C++</strong> Programmers<br />

This section is for current <strong>C++</strong> programmers mov<strong>in</strong>g to <strong>C++</strong>Builder, but will also be of<br />

<strong>in</strong>terest to new <strong>C++</strong> programmers. Although the follow<strong>in</strong>g is not terribly advanced, it is<br />

aimed at <strong>C++</strong> programmers, so if you get lost, muddle your way through and meet us on the<br />

other side.<br />

There are a couple th<strong>in</strong>gs <strong>C++</strong> programmers might f<strong>in</strong>d odd when mov<strong>in</strong>g to <strong>C++</strong>Builder.<br />

First, don’t forget that VCL is Object Pascal and not <strong>C++</strong>. Don’t try to make it <strong>C++</strong>. The<br />

system works very well as long as you understand that and work with<strong>in</strong> those parameters. All<br />

the code you write will be <strong>C++</strong>, but remember that VCL itself is not <strong>C++</strong>. With that <strong>in</strong> m<strong>in</strong>d,<br />

let me give you a couple th<strong>in</strong>gs to consider when us<strong>in</strong>g VCL.<br />

NOTE<br />

All VCL Objects Must Be Allocated Dynamically<br />

When you drop components on a form, <strong>C++</strong>Builder automatically writes code that<br />

dynamically creates the components, so you don’t have to th<strong>in</strong>k about it. However, you may<br />

need to create and use VCL classes at runtime. For <strong>in</strong>stance, let’s say you needed to display<br />

a File Open dialog box and you didn’t have a TFileOpen component on your form. No<br />

problem—you can just create the object on-the-fly. The code would look someth<strong>in</strong>g like this:<br />

TOpenDialog* dlg = new TOpenDialog(this);<br />

dlg->Title = “Open a New File”;<br />

dlg->Execute();<br />

Note that the object must be created us<strong>in</strong>g the new operator. If you attempt to use local<br />

allocation, you will get a compiler error that says VCL classes must be constructed us<strong>in</strong>g<br />

operator new.<br />

Most VCL components can be created at runtime as well as at design<br />

time. It’s easier to create the components at design time because it is<br />

much easier to set the properties us<strong>in</strong>g the Object Inspector than it is to<br />

set the properties through code. Still, there are times when you need to<br />

create components at runtime, and <strong>C++</strong>Builder allows you to do that.<br />

Object Pascal Does Not Support Overloaded Functions;<br />

Hence, VCL Classes Have No Overloaded Constructors<br />

In fact, VCL constructors typically do very little. Take the VCL version of the TRect class,<br />

for <strong>in</strong>stance. In order to construct and set up a TRect object <strong>in</strong> VCL, you would use code like<br />

this:

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

Saved successfully!

Ooh no, something went wrong!