Click here to Skip to main content
15,881,588 members
Articles / Programming Languages / C++

Template Based Arrays of Objects

Rate me:
Please Sign up or sign in to vote.
1.00/5 (17 votes)
10 Jul 2006CPOL2 min read 29.1K   143   14   4
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:

C++
// 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. C++
    // 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. C++
    // 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:

C++
MyClassArray myclassarray;

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

C++
// 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:

C++
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)


Written By
Software Developer (Senior) Texas Capital Bank
United States United States
Professional software engineer with 30+ years of experience delivering systems across diverse industries, looking for the next opportunity to deliver cutting edge end-to-end technology solutions.

Avid reader, disciplined writer and enthusiastic tinkerer with a background in electronics, looking inside and thinking outside the box, genuinely passionate about robust, extensible, reusable and performant code.

Framework developer leading, coaching and learning about best practices, code quality, DevOps and software and data lifecycle management with an agile mindset to create the most elegant and sustainable solutions.

Comments and Discussions

 
Questionhuh ? Pin
Maximilien11-Jul-06 3:03
Maximilien11-Jul-06 3:03 
AnswerRe: huh ? [modified] Pin
hector [.j.] rivas12-Jul-06 12:12
hector [.j.] rivas12-Jul-06 12:12 
GeneralSTL Pin
Shawn Poulson11-Jul-06 1:39
Shawn Poulson11-Jul-06 1:39 
GeneralRe: STL [modified] Pin
hector [.j.] rivas12-Jul-06 12:25
hector [.j.] rivas12-Jul-06 12:25 

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

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