Premiere Issue

C++Builder extensions to C++

by Kent Reisdorph

By now, C++Builder is on the streets and into the hands of thousands of users. Also on the streets is the news that Borland wrote extensions to C++ so that C++Builder could take full advantage of the power of the PME (property, method, and event) model. This news probably elicits one of two responses from you: either "So what?" or "That's bad news!"

Regardless of your initial reaction, it pays to have an informed opinion, so it's important that you get all the facts before you make your final judgment. In this article, we'll examine several significant C++Builder extensions and discuss what they mean to you as a C++Builder programmer.

Standard C++ is alive and well and living in C++Builder

First of all, you need to realize that you can compile any standard C++ code with C++Builder. Yes, Borland added extensions to C++ to maximize the power of the PME model, but that doesn't mean C++Builder isn't an ANSI-compliant C++ compiler. It is. However, I assume you didn't buy C++Builder to write OWL or MFC applications, but rather to take full advantage of the rapid application development (RAD) power C++Builder offers. If you want to utilize C++Builder's RAD capabilities, then you'll have to accept its extensions to C++.

My advice is simple: Don't sweat it. If you're in an environment where a higher power dictates that you must limit your code to ANSI-compliant C++, then you're out of luck as far as C++Builder goes. On the other hand, if you have the freedom to choose your development environment, then I suggest you jump into C++Builder with both feet and not look back.

To extend or not to extend?

It's safe to assume that Borland didn't arrive at a final conclusion without a great deal of thought and research. As I understand it, Borland conducted surveys to ask developers what they wanted in a RAD C++ product. The survey results indicated that plenty of developers would be willing to tolerate a small-scale extension of the C++ language in hopes of RAD coming to C++. The response was strong enough for Borland to determine that producing C++Builder was worth the effort.

As far as I know, all the big compiler vendors (those who produce compilers for the PC, anyway) have extended C++ to suit their own needs. Given that assumption, we're now talking about questions of degree (Borland extended C++ more than Microsoft did), and such discussion can be a particularly fruitless debate.

As I've said, no one is forcing you to use the C++ extensions that C++Builder introduces. The decision to use C++Builder or not is entirely up to you. Just don't get drawn into the idea that the other C++ compilers are 100 percent standards-compliant, because they certainly aren't. (The one possible exception is Borland C++, which has always maintained the high road in the quest for a C++ compiler that adheres to the standards.)

So what are these extensions?

So you want to know what these extensions are all about, do you? All right, let's take a look. C++Builder adds several new keywords to C++:

__automated
__classid
__closure
__int8
__int16
__int32
__int64
__property
__published
Some of these extensions are for C++Builder's internal use only, more or less (__classid, for instance). Others are fairly obvious (__int8, __int16, and so on), so we won't examine each and every one of them. Instead, we'll focus on the most significant extensions. (Note that while the __automated keyword is a significant extension, we can't provide adequate coverage in the space of this article. Therefore, we'll save a discussion of __automated for another time.)

__closure

The __closure extension is easily one of the biggest changes that C++Builder makes to the C++ language. You use the __closure keyword to declare a closure, which is an eight-byte pointer that contains the function address in its first four bytes and the instance of the class where the function resides in its last four bytes. This arrangement makes it possible for you not only to call a function of a particular class, but to call a function in a particular instance of that class, as well. Such a capability exists in Object Pascal (called method pointers in OP), but doesn't inherently exist in C++. Closures are vital for implementing events in VCL. (We'll talk more about events in a future issue.)

__property

You use the __property keyword within a component class declaration to identify a component's properties. A basic property declaration looks like this:

__property int Width;

However, a property declaration can be much more involved than this simple example. For instance, a property might use direct access to read the property's value and a write method when the property is written to. The property might also have a default value. In that case, the property declaration would look something like this:

__property int Width =  {read=FWidth, write=SetWidth, default=200}
As you can see, this code is not C++ as you've previously known it. The __property keyword allows this syntax.

__published

A property can be either published or non-published. If a property is published, the Object Inspector displays it at design time. A nonpublished property doesn't appear in the Object Inspector at design time. (Runtime-only properties are nonpublished.) To declare a property as published, you place its declaration in the __published section of the component's class declaration using code like that shown in Figure A. As you can see, you use the __published keyword just as you use the private, protected, and public keywords in standard C++.

Figure A: You declare a property as published by declaring it in the __published section of the class declaration.

        class MyEditComponent : public TCustomEdit
        {
          private:
          // Private data members and 
          // function declarations here.

          protected:
          // Protected declarations here.
          public:
          // Public declarations here.
          __published:
          // The component's published 
          // properties go here.

	  __property int Width = {read=FWidth, write=SetWidth, default=200}
        };

Conclusion

If you want RAD C++, then you must accept the C++Builder extensions to C++. If you can't live with the C++ extensions, then you can continue with your current Windows programming environment. However, I believe you'll find that the benefits of being able to use RAD make your extra efforts to master the extensions well worth the work.

Kent Reisdorph is a editor of the C++Builder Developer's Journal as well as director of systems and services at TurboPower Software Company, and a member of TeamB, Borland's volunteer online support group. He's the author of Teach Yourself C++Builder in 21 Days and Teach Yourself C++Builder in 14 Days. You can contact Kent at editor@bridgespublishing.com.