Why using the const keyword before and after method or function name?

const T& get_data() const { return data_; }
^^^^^

means it will return a const reference to T (here data_)

Class c;
T& t = c.get_data()             // Not allowed.
const T& tc = c.get_data()      // OK.


const T& get_data() const { return data_; }
                    ^^^^^

means the method will not modify any member variables of the class (unless the member is mutable).

void Class::get_data() const {
   this->data_ = ...;  // is not allowed here since get_data() is const (unless 'data_' is mutable)
   this->anything = ... // Not allowed unless the thing is 'mutable'
}

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)