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

    const operator[]

    In this template
    Code:
    template <class T>
    class dynamic_2d_array
    {
    public:
      dynamic_2d_array(int row, int col) : m_row(row),
                                           m_col(col),
                                           m_data((row != 0 && col != 0) ? new T[row * col] : NULL){}
    
      dynamic_2d_array(const dynamic_2d_array& src) : m_row(src.m_row),
                                                      m_col(src.m_col),
                                                      m_data((src.m_row != 0 && src.m_col != 0) ? new T[src.m_row * src.m_col] : NULL)
      {
        for(int r = 0;r < m_row; ++r)
          for(int c = 0; c < m_col; ++c)
            (*this)[r][c] = src[r][c];
      }
    
      ~dynamic_2d_array()
      {
        if(m_data)
          delete []m_data;
      }
      
      inline T* operator[](int i) { return (m_data + (m_col * i)); }
    
      inline T const*const operator[](int i) const {return (m_data + (m_col * i)); }
    
    
    private:
      dynamic_2d_array& operator=(const dynamic_2d_array&);
      const int m_row;
      const int m_col;
      T* m_data; 
    };
    which I found here: http://www.codeguru.com/forum/showthread.php?t=297838
    is the const operator[] provided specifically to enable a calling function to assign the return value to a const pointer, e.g.
    Code:
    T const * const tp = arr2d[n];
    Or does it serve some other purpose?
    Last edited by r.stiltskin; May 2nd, 2011 at 11:12 AM. Reason: unmarked "resolved"

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: const operator[]

    Quote Originally Posted by r.stiltskin View Post
    Or does it serve some other purpose?
    The const overload allows you to use operator [] on the right side of an equal sign for const objects.

    Comment out the const overload. Once you do that, try to do this:
    Code:
    void foo(const dynamic_2d_array<int>& myArray)
    {
        const int *p = myArray[0];
    }
    You will get a compiler error that you are attempting to call a non-const function (operator []) on a const object.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    May 2011
    Posts
    3

    Re: const operator[]

    That's what I thought (see bottom of my post). I was just wondering if there was some other obscure reason.

    Thanks Paul.

  4. #4
    Join Date
    Aug 2008
    Location
    Scotland
    Posts
    379

    Re: [RESOLVED] const operator[]

    r-stiltskin,

    As far as I can see, there's no need for a const [] operator if all you want do is:
    T const * const tp = arr2d[n];

    However, there are 3 consts in the operator overload:
    Code:
        inline T const*const operator[](int i) const {return (m_data + (m_col * i)); }
    As far as I can see the 2nd one (in bold) has no effect, since it's on the returned type.

    Paul's example requires the 3rd one, since the 2dArray is const.

    Which leaves the 1st one. Is that the one you're asking about?

  5. #5
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: [RESOLVED] const operator[]

    Quote Originally Posted by alanjhd08 View Post
    As far as I can see, there's no need for a const [] operator if all you want do is:
    T const * const tp = arr2d[n];
    As Paul explained, there is a need if arr2d is const.
    In general, the 'need' for the const overload is for the class to be const-correct. If it isn't the code is not reusable, so you should strive to write const-correct code.
    Quote Originally Posted by alanjhd08 View Post
    However, there are 3 consts in the operator overload:
    Code:
        inline T const*const operator[](int i) const {return (m_data + (m_col * i)); }
    As far as I can see the 2nd one (in bold) has no effect, since it's on the returned type.
    It has the 'effect' that you cannot write code like
    Code:
    dynamic_2d_array<int> a;
    // ...
    assert(++a[0] == a[1]);
    Note that, since the pointer is returned by value, any operation on the returned pointer has no effect on the underlying dynamic_2d_array. So, it's really a matter of style whether you declare the returned pointer to be const or not. However, the code should be consistent and use the same style for both const and non-const overload of operator [].
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  6. #6
    Join Date
    Aug 2008
    Location
    Scotland
    Posts
    379

    Re: [RESOLVED] const operator[]

    Hi D_Drmmr,

    Agreed that the const [] operator will be needed, and used if the array is const. My point was that if the array is not const, the const [] operator won't be used.
    The OP suggested that it was required in order
    ..specifically to enable a calling function to assign the return value to a const pointer, e.g.
    Code:
    T const * const tp = arr2d[n];
    which is not the case.

    Thanks for explaining the 2nd const. I got a warning from the Comeau complier saying it would be ignored, so I'm going to go read up on that.

  7. #7
    Join Date
    Oct 2008
    Posts
    1,456

    Re: [RESOLVED] const operator[]

    Quote Originally Posted by D_Drmmr View Post
    It has the 'effect' that you cannot write code like ...
    actually, it would have been an error even if the second const were omitted, because rvalues of scalar types ( that include pointers ) are non modifiable ( hence the Comeau warning experienced by alanjhd08 ) ...

  8. #8
    Join Date
    May 2011
    Posts
    3

    Re: [RESOLVED] const operator[]

    LOL. You all have cleared up my confusion about the third const, and I see that I was mistaken about the first two.

    So now I understand that the third const is needed just in the case that the array itself has been declared const.

    And I see that I all of these assignments
    Code:
    T* tp1 = arr[0];
    const T* tp2 = arr[0];
    const T* const tp3 = arr[0];
    are legal with or without the first and/or second const in the operator.


    And I see that I can't write something like, say
    Code:
    cout << ++arr[0];
    with or without the first and/or second const

    And I see that the first const is needed to prevent using the returned pointer to alter the const array.

    So we're left with the second const which seems to be unnecessary. Agreed?
    Last edited by r.stiltskin; May 2nd, 2011 at 11:19 AM. Reason: corrected last 2 sentences.

  9. #9
    Join Date
    Aug 2008
    Location
    Scotland
    Posts
    379

    Re: [RESOLVED] const operator[]

    Hi,

    And I see that the first const is needed to prevent using the returned pointer to alter the const array.
    Yes, without the first const you could do something like this:
    Code:
    void foo(const dynamic_2d_array<int>& myArray)
    {
        myArray[0][0] = 1;
    }
    which shouldn't be allowed.

    Alan

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