C++Builder 5 Developers Guide

   

The C++ Standard Library

In addition to the VCL and CLX, C++Builder offers one other important library: The C++ Standard Library (formerly known as the Standard Template Library [STL]). Like CLX, in C++Builder 6, this is a cross-platform library with the capability to be used both in Windows and Linux programs.

This is a complex and powerful library, and not all its features can be covered in this short presentation. Instead, here is a look at some of its most important capabilities.

Containers

The template in STL refers to its use of C++ templates to help make sure the classes in the library can create typesafe instances. Nowhere is this more important than when considering container classes.

Containers include

The library also includes adaptors that force certain access patterns on the containers: stack (insert and remove only from the top); queue (insert from one end, remove from the other); and priority queue (same as a queue, but each entry has a priority, and items are ordered by sequence within priority).

Selecting a container can be more complex than you might expect, but you can also use the containers intuitively and attain good results. The help file offers guidance on the more complex aspects of selecting a container.

Using containers is fairly simple as you can see in this example. It uses a vector to store two integers and then copies those integers out to the lines of a VCL memo:

1: vector<int> IntegerVector; 2: 3: IntegerVector.push_back(1); 4: IntegerVector.push_back(2); 5: 6: vector<int>::iterator First = IntegerVector.begin(); 7: vector<int>::iterator Last = IntegerVector.end(); 8: 9: CollectionMemo->Lines->Clear(); 10: 11: while (First != Last) 12: { 13: CollectionMemo->Lines->Add(String(*First)); 14: ++First; 15: };

Line 1 declares the vector as storing integers. Lines 3 and 4 add elements to the vector. Lines 6 and 7 create and initialize iterators that can be used to go from the beginning to the end of the vector. Line 9 clears a TMemo object that is used to display the content of the vector. Line 11 tests the iterators for equality. When they are equal, the iterator has gone past the end of the vector. Line 13 dereferences the iterator, which, thanks to C++ operator overloading, returns the content the iterator has reached; it then converts the integer to a string and puts it in the memo. Line 14 completes the loop by advancing the iterator to the next element of the vector.

Note that this example requires you to #include <vector> and <iterator> .

There are many more features of containers, which can be found in any one of the various books on the STL.

Memory Management

One of the most powerful aspects of using the container classes lies in the memory management they provide for the objects they contain.

When you add an object (rather than a primitive type) to a container, the container uses the copy constructor to create a copy of the object. However, if what is being added is a pointer, it is assumed that the instance is shared with something else, and it is left to the programmer to manage the memory.

The library offers yet another tool for managing memory, and this is a tool that can be especially useful: smart pointers.

Smart pointers are objects that live on the stack, and which will be deallocated when the object passes out of scope. When you allocate an object with new, of course, it lives beyond the scope in which it was allocated. To constrain the scope of memory allocations usually requires the use of a delete that follows the new. For instance, the try / __finally , extension of C++Builder's C++ language is often used to ensure resources allocated from the heap are released.

auto_ptr is the class used for this purpose.

Here's the example:

auto_ptr<TStringList> ListHolder(new TStringList); ListHolder->Add("String");

Note that if you have added objects to the Objects property of a TStringList, you will still be responsible for releasing the memory for those objects manually.

When the ListHolder goes out of scope, whether through normal completion of the function, or through an exception being thrown, the TStringList instance is also destroyed .


   
Top

Категории