Template Classes to Manage Collections of Objects | CodeGuru

Template Classes to Manage Collections of Objects

If you are going to create a project in ATL or some other type, then MFC is often included.  The reason for this is often for a few collection classes and CString.  There will be some overhead in an ATL project if MFC is included and the collection classes is not that hard to rewrite. […]

Written By
CodeGuru Staff
CodeGuru Staff
Mar 21, 1999
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

If you are going to create a project in ATL or some other type, then MFC is often
included.  The reason for this is often for a few collection classes and CString. 
There will be some overhead in an ATL project if MFC is included and the collection classes
is not that hard to rewrite.

I created some templates that I use and it is working fine for me.  The classes are
designed to be fast and use very little memory.  They store pointers to classes and
not objects except one.  If constructors, copy constructors, assignment and other things
are added then the collection class becomes slower.  And it is not much harder to work
with the class if it stores pointers to a class instead of the whole object.  Sometimes
it is easier because you don’t have to add functions needed for the collection class in
object that is managed by it.


Usage Examples


1.   How can I create a string class?
  CBuffer<char,'0'> bufferText;
  bufferText = "This is a text.";
  bufferText += " Some more text";
  int iPosition = bufferText.Find( "Some" );

2.   How can I store some integer values but none of them has the value -1?

  CBuffer<int,-1> bufferInt;

3.   How can I store some pointers?

  CBuffer<void*,NULL> bufferPtr;

4.   I want to store some objects (CMyObject) in a array.

  int iDummy;
  CMyObject* pMyObject;
  CArrayPtr<CMyObject> arrayToMyObject;
  arrayToMyObject.Add( new CMyObject() );
  iDummy = 10;
  arrayToMyObject.Add( new CMyObject( iDummy ) );
  iDummy = 20;
  arrayToMyObject.InsertAt( 1, new CMyObject( iDummy ) );
  pMyObject = arrayToMyObject.GetAt( 0 );
  iDummy = arrayToMyObject.GetSize(); // gets the value 3
  arrayToMyObject.DeleteItems();

5.   I want to store CXxx in a hash.

  CMapPtr<ULONG,CXxx> mapptrToXxx;
  mapptrToXxx.InitHashTable( 21 );
  mapptrToXxx.SetAt( 10, new CXxx );
  CXxx* pXxx;
  if ( mapptrToXxx.Lookup( 10, &pXxx ) == true )
     { ... }
  mapptrToXxx.Empty( true );


Download

Download demo project – 16 KB    templ_coll_demo.zip

Download source – 3 KB    templ_coll_src.zip

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.