CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 2011
    Posts
    1

    Operator [] Overloading

    Hello codeguru members,

    I'm implementing an class to manipulate mathematical matrices and I want it to have an interface as close as mathematical notation as possible.
    So here it is my question:
    can I overload some kind of operator[][] ?
    I would like to access elements like this:

    matrix A(3,3);
    cout << A[0][1];

    Is this possible?
    An solution could be an friend operator[] function that retuns another class, like the matrix_line, and then overloading [] again, this time returning the element, right? But i find this solution deselegant and unclear. Any ideas?

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Operator [] Overloading

    Quote Originally Posted by souza.vss
    can I overload some kind of operator[][] ?
    No.

    Quote Originally Posted by souza.vss
    An solution could be an friend operator[] function that retuns another class, like the matrix_line, and then overloading [] again, this time returning the element, right? But i find this solution deselegant and unclear. Any ideas?
    If you don't want to overload operator[] to return a proxy object for which operator[] is overloaded then overload operator() to accept two arguments.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Operator [] Overloading

    If you make operator[] return a pointer, than the second [] will work as it always does on pointers. However, this gives up the possibility of bounds-checking the second access.

    Also, you should note that several well-respected libraries already provide a mathematical matrix class. For instance, Boost.uBLAS and Eigen.
    Last edited by Lindley; June 10th, 2011 at 08:39 AM.

  4. #4
    Join Date
    Aug 2008
    Posts
    902

    Re: Operator [] Overloading

    Quote Originally Posted by souza.vss View Post
    matrix A(3,3);
    cout << A[0][1];
    I think it would be better to use a template class, and aim for syntax like:

    matrix<3,3> a;
    cout << a(0,1);

  5. #5
    Join Date
    Dec 2010
    Posts
    20

    Re: Operator [] Overloading

    template <typename T>
    class Row
    {
    public:
    const T& operator []( int i);
    //..........
    };


    template <typename T>
    class <Martix>
    {
    public:
    const Row<T>& operator []( int i);
    //..........
    };

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured