Click here to Skip to main content
Click here to Skip to main content

Template Based Arrays of Objects

, 10 Jul 2006 CPOL
Rate this:
Please Sign up or sign in to vote.
Template based arrays of objects.

Introduction

Here's a template based class for dynamic arrays of any type of objects. Credit goes to Michael B. Comet (comet@comet-cartoons.com), who has the Copyright with all rights reserved. I just renamed most of the members and functions and added a Find function.

Usage

These are three steps to use a template based array; to help understand how it works, let's say your project defines a class named MyClass in a file called myclass.h:

// myclass.h header for the MyClass class
class MyClass
{
public:
 int foo;
 int bar;
 
 MyClass();
 ~MyClass();
 
 void DoSomething();
}
  1. Add tarray.h to your project.
  2. Place an include statement for "tarray.h" in myclass.h, which now becomes:
  3. // myclass.h header for the MyClass class
    #include "tarray.h"
    class MyClass
    {
    public:
     int foo;
     int bar;
    
     MyClass();
     ~MyClass();
     
     void DoSomething();
    }
  4. Optionally, add a typedef statement to increase the readability of your code (highly recommended):
  5. // myclass.h header for the MyClass class
    #include "tarray.h"
    class MyClass
    {
    public:
     int foo;
     int bar;
    
     MyClass();
     ~MyClass();
     
     void DoSomething();
    }
    
    typedef CTArray<MyClass> MyClassArray;

Now anywhere in your program where you are in need of an array of MyClass objects, you can declare a variable:

MyClassArray myclassarray;

And access the array in many ways, keeping the [] operator functionality as well as the class functionality:

// add a MyClass object
myclassarray.Append(myclassobject);
 
// access the MyClass object at position 0 members and functions
myclassarray[0].foo = 0;
myclassarray[0].bar = 1;
myclassarray[0].DoSomething();

I've found no limit to the type of objects you can declare a template based array for; you can use it for a simple array of integers as well for your most complicated classes. In C++, things usually get complicated for array classes containing arrays of other classes, but the template based approach works like a charm. In the next code excerpt, I use a template based array of 'facets' owning an array of 'polys', which in turn owns an array of 'vertices' with x, y, z floats:

Facets[nFacet].Polys[nPoly].Vertices[i].x += 1.0f;

and it works! But more to it, is that the IDE's intellisense works, presenting me with all of each class' functions and members after I punch the ".". Beautiful!

Quote

It is an important and popular fact that things are not always what they seem. For instance, on the planet Earth, man had always assumed that he was more intelligent than dolphins because he had achieved so much--the wheel, New York, wars, and so on--while all the dolphins had ever done was muck about in the water having a good time. But conversely, the dolphins had always believed that they were far more intelligent than man--for precisely the same reasons.

HGG 1:23

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

hector [.j.] rivas
Software Developer (Senior) CompuCom, Inc.
United States United States
Hector J. Rivas has 25+ years of experience managing hardware and software development under many different operating systems, platforms and languages. He has developed microcontroller interfaces and PC games; authored computer based training lessons and delivered fully functional financial and administrative data-intensive applications, as well as image processing and other calculation-intensive applications. Mr. Rivas has also managed Y2K remediation, large scale platform migration and Web site projects, from R&D to actual deployment.
Follow on   LinkedIn

Comments and Discussions

 
Questionhuh ? PinmemberMaximilien11-Jul-06 4:03 
AnswerRe: huh ? [modified] PinmemberTor2k12-Jul-06 13:12 
GeneralSTL PinmemberShawn Poulson11-Jul-06 2:39 
GeneralRe: STL [modified] PinmemberTor2k12-Jul-06 13:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141220.1 | Last Updated 10 Jul 2006
Article Copyright 2006 by hector [.j.] rivas
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid