Click here to Skip to main content
Licence CPOL
First Posted 10 Jul 2006
Views 13,634
Downloads 82
Bookmarked 13 times

Template Based Arrays of Objects

By | 10 Jul 2006 | Article
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)

About the Author

hector.j.rivas

Software Developer (Senior)

United States United States

Member

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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Questionhuh ? PinmemberMaximilien3:03 11 Jul '06  
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.
 
Confused | :confused: and me think it's a little bit insulting to the original author ?
 
Why did you not simply write that you added only one new function to someone else's code ?
 
Confused | :confused:
 

Maximilien Lincourt
Your Head A Splode - Strong Bad
AnswerRe: huh ? [modified] PinmemberTor2k12:12 12 Jul '06  
GeneralSTL PinmemberShawn Poulson1:39 11 Jul '06  
GeneralRe: STL [modified] PinmemberTor2k12:25 12 Jul '06  

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.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120529.1 | Last Updated 10 Jul 2006
Article Copyright 2006 by hector.j.rivas
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid