Making a class function const

How would I go about making this code const? Its part of a 2D array that inherits a single dimensional array and manipulates that array so that you believe it is 2D. I can post the classes if need be =).

1
2
3
4
5
6
7
8
template <typename T>
Row<T> Array2D<T>::operator [] (const int index) const
{
    if(index >= m_row)
        throw Exception("Index exceeds amount of rows");
 
    return Row<T>(*this, index);
}
Last edited on
By adding a const after the parameter list?
Besides, shouldn't it be if (index >= m_row)?
I did change that index part haha sorry. and the error I get is.

error C2665: 'Row<T>::Row' : none of the 2 overloads could convert all the argument types
closed account (zb0S216C)
Are any of the parameters of the constructor of Row<T> references? Judging by the error, the compiler is unable to convert the arguments to the types the constructor of Row<T> requires.

Wazzak
Last edited on
Since you are doing an index check you might as well check for for negative numbers as well. It never hurts to make your code as idiot proof as possible.

As for the return value. If you return a class using a constructor I don't think you can make the method const because you are obviously altering the Row<T> in the constructor.

If you are getting a cannot have cv qualifier error then that is probably the case. Just remove the const since you are actually modifying data in that method.
Last edited on
Topic archived. No new replies allowed.