C Language

   

C++ Language

The C++ language is at the core of C++Builder. C++Builder offers a high level of support for this standardized programming language.

ANSI Compliance

The C++ language was the next step in the development of the Bell Labs C language. And, like C, C++ gained widespread acceptance.

Widespread acceptance is a good thing because it leads to competition among vendors of compilers and development environments. But, as those vendors struggle for technological dominance , they often produce unique language features, which, although useful, mean that programs written for one compiler might not be able to be compiled by another.

The American National Standards Institute (ANSI) was founded in 1918, and is a private, nonprofit organization that coordinates the definition and publication of voluntary industry standards in a variety of fields. As such, they are the perfect organization to take on the problem of standardizing both the C and C++ languages.

NOTE

The document available at their Web site (http://webstore.ansi.org/ansidocstore/product.asp?sku=ISO%2FIEC+14882%3A1998) is the result of this effort, which was completed in 1998.


In the 21 st century, "ANSI compliance" is a critical factor sought by developers who want to have the freedom to develop and compile using several different development systems, or who need to target more than one operating system or CPU instruction set.

Borland offers a powerful set of proprietary extensions to C++ so that they can provide the component-oriented features of the Visual Component Library class framework. But, they also provide what might be the highest level of ANSI compliance in the industry. You can force the compiler to only accept ANSI-compliant programs by clicking a check box in the development environment (pick the menu entry Project, Options; in the dialog box, click the Advanced Compiler tab; and under Language compliance, choose ANSI), as shown in Figure 1.2.

Figure 1.2. Project Options showing the ANSI Compliance option set.

graphics/01fig02.gif

To be safe, under Source, make sure to leave Nested comments and MFC compatibility unchecked.

Keep in mind that choosing ANSI compliance means that you cannot develop Windows programs because there are many features of the Windows operating system that cannot compile under ANSI compliance. You also cannot use Borland's Visual Component Library (VCL). You can, however, create programs that use streams for input and output, which are usually referred to as console programs.

Microsoft Compatibility

When a program is compiled without ANSI compatibility (for instance, with the Borland compatibility option selected), it will compile programs using Windows features. This is the basic level of Microsoft compatibility.

But, some programs won't compile.

For instance, programs that use the older Microsoft Foundation Classes (MFC) will not compile without using the MFC compatibility check box on the Advanced Compiler page.

This relaxes numerous rules in the compiler, including

  • Not allowing spurious semicolons in a class scope

  • Not allowing anonymous structs

  • Not using the old-style scoping resolution for loops

  • Not allowing methods to be declared with a calling convention, where the declaration leaves off the calling convention in the definition

  • Not trying the operator new if it cannot resolve a call to the operator new

  • Not letting you omit the operator and on member functions

  • Not allowing a const class that is passed by value to be treated as a trivial conversion, not as a user conversion

  • Not allowing you to use a cast to a member pointer as a selector for overload resolution when the qualifying type of the member pointer is not derived from the class in which the member function is declared

  • Not accepting declarations with duplicate storage in a class

  • Not accepting and ignoring #pragma comment(linker, ",") directives

In addition to this, you need to link your program with the nafxcw.lib MFC compatibility library that comes with C++Builder. This occurs automatically as a result of selecting MFC compatibility when compiling with the C++Builder-development environment, but requires a special flag if compiling and linking from the command line (the VF option).

Using MFC with the VCL has proven to be somewhat more difficult, in that specific header file changes have typically needed to be made to avoid conflicts between the names used by the MFC and by the VCL. These are documented at http://www.temporaldoorway.com/programming/cbuilder/otherlibrary/usingvclandmfc.htm for C++Builder versions through 4.

Another level of Microsoft compatibility lies in the capability to import a Microsoft Visual C++ project directly into the C++Builder development environment. Just open it and compile it.

To permanently convert the project, you can use the VCTOBPR utility, which will turn Visual C++ project and workspace files into their C++Builder equivalents.

Recommended Language References

Learning about C++ can be difficult, but the right books and online materials can make things easier.

  • For reference, of course, the ANSI standard document is the definitive source of "language law," along with the documents available via the Web site (http://www.research.att.com/~bs/C++.html) of C++ originator Bjarne Stroustrup (though Stroustrup is a more enjoyable writer than the boards who compose standards).

  • You can also get his newly revised book at sites such as Amazon.com (http://www.amazon.com/ exec /obidos/ASIN/0201700735/104-5455331-5996736).

For less formal material, Sams publishes several excellent language instruction books, including

  • Sams Teach Yourself C++ in 10 Minutes (recently revised), which breaks up C++ into manageable, easily understood lessons; great for getting a rapid, yet comprehensive exposure to the C++ language.

  • Sams Teach Yourself C++ in 21 Days (recently revised), which gives three weeks of detailed instructional material on every aspect of C++.

Borland Language Extensions and Standard Objects

Borland's C++Builder integrates the Delphi Visual Component Library into its use of the C++ language. The problem is that Delphi is a Pascal-based language, whereas C++ is a C-based language. So, Borland used the ANSI-approved method of creating vendor-specific extensions, and added features to C++.

Properties

Object-oriented languages provide member variables to retain the state of the object during its lifetime. But when those member variables are directly exposed beyond the object, other parts of the program can disrupt the object by setting values that are inconsistent.

To overcome this problem, many object-oriented developers create member functions, called getter and setter functions, and hide access to the member variables within these member functions. This enables the programmer to be able to edit values that will be assigned to the member variable and generate an exception or other error condition when an incorrect value is provided. It also enables the programmer to hide the type of the internal variable, and even to provide a variety of hidden implementations for the member variable's storage (such as a file, a database, or even a complex computation).

A variety of problems exist with getters and setters, one of which is that they look a lot different from member variables in calculations.

For instance

 int Something =     SomeObject.GetMember() +     SomeOtherObject.GetMember(); 

Another is that you don't really assign to a getter function ”you pass a variable or expression as an argument to the function, which is equivalent to assignment.

 SomeObject.SetSomeMember  (     SomeObject.GetMember() +     SomeOtherObject.GetMember()  ); 

When Borland created the Delphi language, they provided properties as a way to offer getters and setters as an implicit part of a class ”giving the advantages of getters and setters combined with the expressiveness of member variable use.

In a C++Builder class, a property usually consists of a member variable declaration (private), a getter and setter function (protected), and a property declaration (public).

 class aClassWithAProperty  {     private:        int myMemberVariable;     protected:        int __fastcall GetMemberVariable(void)        {           return myMemberVariable;        };        void __fastcall SetMemberVariable(int theMemberVariable)        {           myMemberVariable = theMemberVariable;        };     public:     __property int MemberVariable =     {        read=GetMemberVariable,        write=SetMemberVariable     }; 

Of course, you can use an instance of this class and its property as shown below:

 AClassWithAProperty ClassWithAProperty;  ClassWithAProperty.MemberVariable = 2;  int Something = ClassWithAProperty.MemberVariable; 

Any code is allowed in the getter and setter.

Properties are most often used in C++Builder components . To help support the visual environment provided by C++Builder, Borland has created a special section for the class (identified by __published ), just like public and private . If a property is in the __published section, it will appear in the C++Builder object inspector where it can be modified. In addition, when a project is saved, the values of component properties that are in the __published section of the component class are also saved for every instance of the component class.

The following is the earlier class, changed into a component (note the __published section, which now contains the __property declaration):

 class aClassWithAProperty: public TComponent  {     private:        int myMemberVariable;     protected:        int __fastcall GetMemberVariable(void)        {           return myMemberVariable;        };        void __fastcall SetMemberVariable(int theMemberVariable)        {           myMemberVariable = theMemberVariable;        };     __published:        __property int MemberVariable =        {           read=GetMemberVariable,           write=SetMemberVariable        };  }; 
Delphi-Style Default Properties

The Delphi language supports a property that is the default property for the class when you use the subscript operator. In earlier versions of C++Builder, you needed to use awkward constructs like

 StringListVariable->Strings[Index] 

Now you can simply use

 (*StringListVariable)[Index] 

try/finally

The Delphi language introduced a feature to exception handling that was later adopted in Java. The C++Builder extensions to C++ include this extension.

Called __finally (the two leading underscores are required by the ANSI standard to indicate an extension), this extension marks the part of an exception-handling block that will be executed regardless of whether an exception occurs. It is particularly useful for cleaning up dynamically allocated storage.

 TStringList *List = new TStringList;  try  {     // Do something  }  __finally  {     delete List;  }; 

The code in the __finally block is executed when the try block is completed, or when an exception occurs.

This sort of statement can be inside or outside of exception handling code, but is usually inside so that resources can be released before an exception is handled.


   
Top


C++ Builder Developers Guide
C++Builder 5 Developers Guide
ISBN: 0672319721
EAN: 2147483647
Year: 2002
Pages: 253

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net