Sunday, October 28, 2012

Back to basics: write a matrix of generic types

Imagine to build a matrix [MxN] of generic type. Implement the classic [x, y] operator for accessing the element [x, y]. How would you implement it in C++?

here the code, where size can be either allocated at compile time or at run-time.

do you have any alternative solution? yes, boost::matrix is a valid one ;)

1 comment:

  1. I solved this problem in a slightly different way for Luminance HDR (my open source project).

    My solution gives an iterator to a row for the first operator[], and then the iterator is a direct access one, so it does have an operator[] as well.

    Hence the notation var[i][j] will give you the element j of the row i.

    My code is here: http://sourceforge.net/p/qtpfsgui/code/ci/820f3cc38f275942ac0df6af1293dc37f93970bb/tree/src/Libpfs/array2d.h

    ReplyDelete