[Tfug] Need help with a C++ algorithm

Joe Blais joe.blais at pti-instruments.com
Thu Mar 15 18:58:58 MST 2007


Hello -

> What I ended up doing is writing a template class that just
> stores a pointer

Templates are nice and fun but since you actually have access to where the
thing is created, such that you can even write a template, why not just keep
a copy of the size and use that as an additional parameter in any of the
methods that use the pointer -- kind of like strncpy, memcpy and those c
things?

int miSize = random_size ;
myclass * mystuff = new myclass[miSize] ;

then make your method like --
void DoThis( myclass * pMC, int iSiz ) // or myclass pMC[]
{ for ( int i = 0 ; i < iSiz ; ++i ) doittoit( pMC[i] ) ; } ;


and call it like
DoThis( mystuff, miSize ) ;

then
delete [] mystuff ;

Unless of course you make a guard object with your template that guarantees
the delete.





More information about the tfug mailing list