Webocreation

Monday, November 23, 2009

Creating Class Templates

Creating Class Templates

Class templates are useful for creating abstract data types, which can then be used to create classes based on specific data types. For example, you can use a linked list template to parameterize the data that is held at each of the link list's nodes.

You can use class templates to create collection templates, create new data types, and modify existing data types.

This section includes the following topics:

Class Template Syntax

Class templates are composed of three elements:

® The template keyword
® A comma-delimited list of formal parameters, enclosed in angle brackets (<>)
® The class definition


A class that is generated from a template is sometimes called a templated class.

You can use class templates to modify existing classes. For example, you can define a class template that adds reference counting to an object as follows:

template <class T>

class TCount

{

int m_count;

T m_type;

public:

TCount( T type ) { m_type = type; m_count = 0; }

int IncrementCount() { return ++ m_count; }

int GetCount() { return m_count; }

T& GetContents() { return m_type; }

};



In this example, the template accepts one parameter, a type T. The generated class has one constructor, one method, and two assessor functions. This template would be useful for keeping count on an object. You could also use this template as a basis for creating smart pointers, which are pointers that track the references on them and automatically delete themselves when there are no more references.

The formal parameters in a template definition can be any legal C++ data type that is consistent with the template design. In addition to using built-in and user-defined data types, you can use other class templates as parameters in a template instantiation. For example, you can define a queue of counted strings as follows:

CQueue< TCount<CString> > m_Queue;


Note When using a template as a parameter, be sure to include a space between the closing brackets. For more information, see Function Template Syntax in this chapter.



  1. Instantiating Class Templates



Unlike function templates, class templates must be instantiated explicitly. To instantiate a class template, you provide the arguments for the template class. The following example code creates an instance of TCount that uses CString as the data type:

// Create an object called stringObject

TCount<CString> stringObject("test");

// Bump the count

stringObject.IncrementCount();



No code is generated for a class template until the template is instantiated by the creation of a variable. Moreover, member functions of the class are instantiated only if they are invoked by code somewhere in the application.


Using Collection Templates



Collections are useful for implementing the data structures that define your document classes in the application framework.

MFC collection classes provide member functions for managing the elements in collections. MFC includes templated and non-templated collection classes.

This section includes the following topics:



  1. Categories of Collections


MFC collection classes are organized into three main types, based on the data structures that are used. To see an illustration that shows these types, click this icon.





Arrays

An MFC array is a dynamically sized grouping of elements that are directly accessible through a zero-based integer subscript.

Lists

A list is an ordered grouping of elements. Use lists to implement queues and stacks.

You can add new elements at the beginning or end of a list, or before or after a specified element. You can traverse the list forward or backward.

Maps
A map is a dictionary or associative array that maps keys to values. Duplicate keys are not allowed.
Choosing a Collection Type for Your Application
The following table compares the characteristics of the three main collection types.

Characteristic List Array Map

Can the collection be ordered? Yes Yes No

Is the collection indexed? No By integer By key

Can elements be inserted quickly? Yes No Yes

Can elements be searched for quickly? No No Yes

Can the collection include duplicate elements? Yes Yes No (keys) Yes (values)

The three most commonly used collection classes are the templated collections CList, CArray, and CMap.

MFC Templated Collection Classes

MFC provides two groups of templated collection classes. One group, including CArray, CMap, and CList, is used to directly manipulate data structures. The others, CTypedPtrArray, CTypedPtrList, and CTypedPtrMap, are used to wrap other pointer classes to provide type safety and to minimize the amount of casting that is required.

CArray, CList, CMap

The MFC templated versions of the array, list, and map classes are CArray, CList, and CMap. These templates can hold a variety of objects. They can also hold pointers to objects. The template parameters determine the types of objects that the template can store. Because the CArray, CList, and CMap classes are derived from the CObject class, they inherit properties of CObject, such as serialization and dynamic creation.

With the templated versions of the collection classes, you can create specific instances suited to your data-storage requirements. To see sample code that shows the syntax for creating a collection based on a templated class, click this icon.





The following example code creates a collection based on CArray:

class myClass;
CArray <myClass, myClass&> myArray;
myArray.SetSize(5000, 1000);

Typed-Pointer Classes

The second group of templated classes in MFC allows type-safe wrapping of collections that contain pointers. The classes in this group are:

® CTypedPtrArray

Stores pointers in an array.

® CTypedPtrList

Stores pointers in a linked list.

® CTypedPtrMap

Maps keys to values. Both keys and values are pointers.

The typed-pointer array and list classes, CTypedPtrArray and CTypedPtrList, take two parameters: base_class and type. These classes can store any data type, which you specify in the type parameter. Each class is derived from a non-templated collection class that stores pointers. You specify this base class in base_class. For arrays, use either CObArray or CPtrArray. For lists, use either CObList or CPtrList.

The following example code creates a type-safe list, based on CObList, of CMyData objects:

CTypedPtrList<CObList, CMyData*> m_myList;

For more information about CObList, search for "CObList" in Visual C++ Help.

MFC Non-Templated Collection Classes

MFC versions prior to 3.0 had 17 non-templated collection classes. They have been superseded by the more encompassing templated collections, but are retained for reasons of backward compatibility and simplicity.

Type of collection Class name


Arrays CByteArray

CDWordArray

CObArray

CPtrArray

CStringArray

CWordArray

CUIntArray


Lists CObList

CPtrList

CStringList

Maps CMapPtrToWord

CMapPtrToPtr
CMapStringToOb
CMapStringToPtr
CMapStringToString
CMapWordToOb
CMapWordToPtr

For more information about any of these collection classes, search Visual C++ Help.

Debugging TemplatesThe easiest way to debug a template is to code and debug a specific class or function based on the template. Once you have tested the specific class or function, you can parameterize the targeted data and then continue to test the template design.

No comments:

Post a Comment