Jump to content
  • Advertisement
Sign in to follow this  
BrianTheLlama

Overloading [ ][ ]

This topic is 4804 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is it possible to overload the array access operator [] to provide multidimensional access. For example, if I had a matrix class:
class Matrix
{
 private:
         float **elements;
};

I need a method to access a reference to the elements of the array so that I can simply say

Matrix myMatrix;

myMatrix[2][1] = 3;


Anyone know if this can be done? Adam EDIT: Whoops, wrong tags!

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
it can be because the std::string class does it. just dunno how u'd go about this, try looking at the string header

Share this post


Link to post
Share on other sites

#include<cstddef>

template<std::size_t rows, std::size_t columns>
class matrix
{
public:

float (&operator[](std::size_t rowindex))[columns]
{
return elements[rowindex];
}

private:

float elements[rows][columns];

};

int main()
{
matrix<4,4> thematrix;
thematrix[1][2] = 0;
}




Note: you may need to use int or unsigned int instead of std::size_t if you use 2003.net, as there seems to be a compiler bug that prevents the above code form compiling with size_t.

Share this post


Link to post
Share on other sites
You could use the operator () like the D3DXMATRIX class does:


// access grants
FLOAT& operator () ( UINT Row, UINT Col );
FLOAT operator () ( UINT Row, UINT Col ) const;

Share this post


Link to post
Share on other sites
You can't overload operator[][] because no such operator exists, but you can overload operator[] to return a proxy object which also overloads operator[]:
#include <iostream>
#include <stdexcept>
#include <vector>

/* poorly designed Matrix class just as an example. Obviously you'd want to
overload with operator[] const too. This version may also suffer from
iterator invalidation, depending on whether your matrix is resizeable or not.
*/

class Matrix
{

private:

class MatrixHelper;

public:

Matrix();

// other matrix operations

MatrixHelper operator[](unsigned int index);

private:

std::vector< float > elements_;
unsigned int width_;

class MatrixHelper
{

public:

MatrixHelper(std::vector< float >::iterator rowIterator, unsigned int width);
float & operator[](unsigned int index);

private:

std::vector< float >::iterator rowIterator_;
unsigned int width_;

};
};

Matrix::Matrix()
:
elements_(9),
width_(3)
{
// set to identity
elements_[0] = elements_[4] = elements_[8] = 1;
}

Matrix::MatrixHelper Matrix::operator[](unsigned int index)
{
if (index >= elements_.size() / width_)
{
throw std::out_of_range("invalid matrix row index");
}
return Matrix::MatrixHelper(elements_.begin() + (index * width_), width_);
}

Matrix::MatrixHelper::MatrixHelper(std::vector< float >::iterator rowIterator, unsigned int width)
:
rowIterator_(rowIterator),
width_(width)
{
}

float & Matrix::MatrixHelper::operator[](unsigned int index)
{
if (index >= width_)
{
throw std::out_of_range("invalid matrix column index");
}
return *(rowIterator_ + index);
}

int main()
{
Matrix m;
std::cout << "m[1][1] = " << m[1][1] << '\n';
m[1][1] = 7;
std::cout << "m[1][1] = " << m[1][1] << '\n';
std::cout << "m[0][1] = " << m[0][1] << '\n';
}



Enigma

Share this post


Link to post
Share on other sites
Cheers guys, those suggestions where really helpful!

Due to the relative simplicity I think I'll go with using parenthesis to access the matrix elements!

Thanks to everyone who contributed!

Adam

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!