Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why write two () operators

Tags:

c++

Why do it twice? These 2 lines, why do it like this way? Is one enough?

inline T& operator() (int row, int col) { return this->m_data[row*NC + col]; }

const inline T& operator() (int row, int col) const { return this->m_data[row*NC + col]; }

thank you

 *
 * 2-DIMENSIONAL ARRAY
 *
 * Simulated by 1-dimension array.
 ******************************************************************************/

#ifndef __2D_ARRAY_H__
#define __2D_ARRAY_H__
#include <stdint.h>
#include <stdlib.h>

namespace alg {
    /**
     * 2D Array definition
     */
    template <typename T=char>
        class Array2D {
            private:
                uint32_t NR;        // num of rows
                uint32_t NC;        // num of columns
                T * m_data;         // the place where the array resides.

            public:
                /**
                 * construct an array of size [nrow,col]
                 */
                Array2D(uint32_t nrow, uint32_t ncol) {
                    NR = nrow;
                    NC = ncol;  
                    m_data = new T[nrow*ncol];
                }

                /**
                 * destructor
                 */ 
                ~Array2D() {
                    delete [] m_data;
                }

            private:
                Array2D(const Array2D&);    
                Array2D& operator=(const Array2D&); 

            public:

                /**
                 * return number of rows of this array
                 */
                inline const uint32_t row() const { return NR; }
                /**
                 * return number of columns of this array
                 */
                inline const uint32_t col() const { return NC; }

                /**
                 * return the value by the given (row, col);
                 */
                inline T& operator() (int row, int col) { return this->m_data[row*NC + col]; }
                const inline T& operator() (int row, int col) const { return this->m_data[row*NC + col]; }

                inline T* operator[] (int row) { return &(m_data[row * NC]); }
                inline const T* operator[] (int row) const { return &(m_data[row * NC]); }

                /**
                 * clear the array by a given value
                 */
                void clear(const T & value) {
                    for(uint32_t i=0; i<NR*NC;i++){
                        m_data[i] = value;
                    }
                }
        };
}

#endif //
like image 461
BufBills Avatar asked Nov 22 '13 19:11

BufBills


People also ask

Why is || used in C?

Logical OR (||) operator in C Logical OR is denoted by double pipe characters (||), it is used to check the combinations of more than one conditions; it is a binary operator – which requires two operands.

What is the use of || operator?

The logical OR operator ( || ) returns the boolean value true if either or both operands is true and returns false otherwise.

What does || mean in JS?

The logical OR ( || ) operator (logical disjunction) for a set of operands is true if and only if one or more of its operands is true. It is typically used with boolean (logical) values. When it is, it returns a Boolean value.


2 Answers

One is const and the other is not.

The difference is that when you have a const reference to a Array2D you're only allowed to call member functions marked const. In this case that means the 2nd version, where it must necessarily return a const reference to the element it holds.

If you have a non-const reference, though, that 2nd version means you can't use operator() to make any changes to your Array2D.

If you look at standard library containers like std::vector you'll see that they do the same thing. You can get an iterator from begin() and a const_iterator from begin() const.

like image 151
Adam Avatar answered Nov 15 '22 06:11

Adam


The first, non-constant version returns a reference that can be modified. If you have a constant object, you still want to be able to at least read the value, so you have to provide a second constant version.

like image 42
Philipp Claßen Avatar answered Nov 15 '22 06:11

Philipp Claßen